4 ms·
A generic representation I guess means primitives, dictionaries and arrays. Stringly typed.
by the_gipsy 8y ago
A generic representation I guess means primitives, dictionaries and arrays. Stringly typed.
- coldtea 8y agoNo -- "primitives, dictionaries, and arrays" typed, which is different from "stringly typed", and representative of what JSON actually is. JSON doesn't represent class hierarchies, but plain nested JS objects. And the types it supports are just dictionary, array, string, boolean, and float. So having all the ceremony in Swift (or similar in Go, etc) to parse into predefined structs is not really idiomatic to JSON. You can have all of those in the generic representation -- and this is exactly how any dynamic language does it (JS, Python, PHP, etc.) -- and any statically typed language when it wants to read arbitrary JSON.
- the_gipsy 8y agoJSON does not represent class hierarchies, but neither dictionaries. It represent structures, 99% of the time the exact shape and format is public. Parsing JSON into predefined structs is indeed very idiomatic. A dictionary is a key/value map, period. If you want to use that, because you don't want to do the encode/decode ceremony, then you are doing stringly-typed code in a statically typed language. This is not bad per-se, as you say dynamic languages always do this, but you should recognize what you are doing.
- coldtea 8y ago>A dictionary is a key/value map, period. If you want to use that, because you don't want to do the encode/decode ceremony, then you are doing stringly-typed code in a statically typed language. It's not stringy-typed since all the scalars have the proper type as they did on the "wire". In JSON you have string, booleans, floats, objects, arrays, and null. And JSON is not a general serialization format (e.g. all object keys are strings, all numeric values are floats), so this "encode/decode" ceremony just introduces a layer that isn't there to begin with (and would fail without custom decoders for things like dates and so on). In general the decode is really a mapping for convenience and schema validation, than an actual decode of what the JSON contains (since JSON knows nothing about those structs as structs).
- the_gipsy 8y agoi never referred to values, but the structure. hammering json into a dict is stringly-typed. it is stringly typed when you do foo[“bar”][“baz”] etc. foo.bar.baz would be typesafe.