Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JSONDecoder and JSONEncoder classes thread-safe?

i couldn't find any evidence that would suggest either way.

i'm currently creating new instances each time and configuring them with custom decoding/encoding options, and wondered if i could create a singleton and vend it each time i need it.

like image 946
damacster Avatar asked May 09 '18 15:05

damacster


1 Answers

Judging by the code for JSONDecoder and JSONEncoder they seem to be thread-safe.

Both encode() and decode() use JSONSerialization (which has been thread-safe since iOS 7 and macOS 10.9), and both methods create their own private decode/encode objects in local variables. There is no shared storage as far as I can see.

The code can be found here: https://github.com/apple/swift-corelibs-foundation/blob/master/Sources/Foundation/JSONEncoder.swift

like image 95
Bjørn Olav Ruud Avatar answered Nov 06 '22 08:11

Bjørn Olav Ruud