What Do JSONserialization Options Do and How Do They Change JSONresult

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 mutable NSArrayNSMutableArray and NSDictionaryNSMutableDictionary.
  • 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.

JsonOptionsmight 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



Leave a reply



Submit