Why does Boost property tree write_json save everything as string? Is it possible to change that?

Ok, I’ve solved it like this, (of course it won’t suite for everybody, as it is a bit of a hack, that need further work). I’ve wrote my own write_json function (simply copied the files, json_parser.hpp and json_parser_write.hpp to my project) and modified the following lines in json_parser_write.hpp: commented line 37 – escaping the quote … Read more

How to parse JSON in golang without unmarshaling twice

One solution is to partially unmarshal the data by unmarshalling the values into a json.RawMessage instead of an interface{} var myMap map[string]json.RawMessage Later in the switch, which still is required, you do not need to marshal. Just do: err = json.Unmarshal(v, &myAck) Playground: https://play.golang.org/p/NHd3uH5e7z

Decoding Error — Expected to decode Dictionary but found an array instead

[{“id”:”1″,”name”:”sobande_ibukun”,”member”:”blue”}] The [] around denotes that it is an array. Decode with the following and it should work: let courses = try JSONDecoder().decode([Course].self, from: data) If you are sure that it will always be one course you can do: print(courses.first!.name) If there may be many courses you can print every name: courses.forEach { course in … Read more

Swift Codable with dynamic keys

Assuming you left out the { and } that would surround this block and are required for this to be valid JSON, the following is the simplest solution to getting things parsed, you really don’t need to deal with CodingKey at all since your names match the keys in the JSON, so the synthesized CodingKey … Read more

Encode/Decode Array of Types conforming to protocol with JSONEncoder

The reason why your first example doesn’t compile (and your second crashes) is because protocols don’t conform to themselves – Tag is not a type that conforms to Codable, therefore neither is [Tag]. Therefore Article doesn’t get an auto-generated Codable conformance, as not all of its properties conform to Codable. Encoding and decoding only the … Read more

Postgres JSON data type Rails query

For any who stumbles upon this. I have come up with a list of queries using ActiveRecord and Postgres’ JSON data type. Feel free to edit this to make it more clear. Documentation to the JSON operators used below: https://www.postgresql.org/docs/current/functions-json.html. # Sort based on the Hstore data: Post.order(“data->’hello’ DESC”) => #<ActiveRecord::Relation [ #<Post id: 4, … Read more

.NET Core: Remove null fields from API JSON response

.NET Core 1.0 In Startup.cs, you can attach JsonOptions to the service collection and set various configurations, including removing null values, there: public void ConfigureServices(IServiceCollection services) { services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings .NullValueHandling = NullValueHandling.Ignore; }); } .NET Core 3.1 Instead of this line: options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; Use: options.JsonSerializerOptions.IgnoreNullValues = true; .NET 5.0 Instead of … Read more