What do JSONSerialization options do and how do they change jsonResult?
Short answer for the first two options:
Ignore them in Swift!
In Swift you can make objects mutable just with the var
keyword.
In Objective-C on the other hand you need
NSJSONReadingMutableContainers
to make the nested collection types mutableNSArray
→NSMutableArray
andNSDictionary
→NSMutableDictionary
.NSJSONReadingMutableLeaves
to make the value strings mutable →NSMutableString
.
In both Objective-C and Swift if you are only reading the JSON you don't need mutability at all.
The third option NSJSONReadingAllowFragments
is important if the root object of the received JSON is not an array and not a dictionary.
If it is an array or dictionary you can omit that option, too.
The pair of empty brackets []
represents No options
(the options
parameter can be omitted in Swift 3+).
option[] in JSONSerialization
JSONSerialization.WritingOptions
is an OptionSet
and conforms to the ExpressibleByArrayLiteral
protocol, which means that a “set of options” can be specified as an array literal. For example:
let jsonData = try JSONSerialization.data(withJSONObject: json,
options: [.prettyPrinted, .sortedKeys])
In particular, an empty array literal means “no option”:
let jsonData = try JSONSerialization.data(withJSONObject: json, options: [])
In this particular case it would be equivalent to omitting the parameter
let jsonData = try JSONSerialization.data(withJSONObject: json)
because it has an default value of []
:
class func data(withJSONObject obj: Any,
options opt: JSONSerialization.WritingOptions = []) throws -> Data
Swift3 JSONSerialization with [String:Any] rather than NSDictionary
Are there drastic efficiency differences
Yes there are.
NSDictionary
completely lacks type information, native Swift collection types are much more efficient and highly recommended. And you get mutability for free using var
. mutableContainers
are useless in Swift anyway.
jsonObject(with data
returns Any
because the return type can be Dictionary
, Array
or even String/Number
, the least common denominator is Any
, cast it to the expected type.
asp .net core 6 how to update option for json serialization. Date format in Json serialization
We can try to use builder.Services.Configure<JsonOptions>
to set our Serializer setting in DI container from .net core 6
JsonOptions
let us configure JSON serialization settings, which might instead AddJsonOptions
method.
JsonOptions
might use same as JsonOptions
object from DI in the SourceCode.
using Microsoft.AspNetCore.Http.Json;
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.Converters.Add(new DateTimeConverterForCustomStandardFormatR());
});
I think this change is based on Microsoft wanting to introduce minimal web API with ASP.NET Core in .net 6
Minimal APIs are architected to create HTTP APIs with minimal dependencies. They are ideal for microservices and apps that want to include only the minimum files, features, and dependencies in ASP.NET Core.
Related Topics
React Native iOS Build:Can't Find Node
How to Change Color of Uitableviewcell When Selecting
How to Create a Glow Around a Sprite via Skeffectnode
Ios: Delete All Core Data Swift
How Does Google's Custom iOS Keyboard, Gboard, Programmatically Dismiss the Frontmost App
Export Compliance in iOS App Submission
How to Check If a Framework Is Bitcode Supported for Xcode7
Dynamic Uiimageview Size Within Uitableview
View at the Bottom in a Uiscrollview, with Autolayout
Uitextfield - Capture Return Button Event
Multiline Uilabel with Adjustsfontsizetofitwidth
Our App Crashed in iOS 9 Which Upload by Xcode 10
Getting Video Snapshot for Thumbnail
How to Apply Filter to Video Real-Time Using Swift
How to Detect Live Changes on Textfield in Swiftui
How to Get 1 Hour Ago from a Date in iOS Swift