7 ms·
> Schemas Are Awesome No reason you can't implement schemas over JSON. In fact, you typically implicitly do - what your code is expecting to be present in the
by falcolas 12y ago
> Schemas Are Awesome
No reason you can't implement schemas over JSON. In fact, you typically implicitly do - what your code is expecting to be present in the data structures deserialized from JSON.
> Backward Compatibility For Free
JSON is unversioned, so you can add and remove fields as you wish.
> Less Boilerplate Code
How much boilerplate is there in parsing JSON? I know in Python, it's:
structure = json.loads(json_string)
Now then, if you want to implement all kinds of type checking and field checking on the front end, you're always welcome to, but allowing "get attribute" exceptions to bubble up and signal a bad data structure have always appealed to me more. I'm writing in Python/Ruby/Javascript to avoid rigid datastructures and boilerplate in the first place most times.
[EDIT] And for languages where type safety is in place, the JSON libraries frequently allow you to pre-define the data structure which the JSON will attempt to parse into, giving type safety & a well define schema for very little additional overhead as well.
> Validations and Extensibility
Same as previous comment about type checking, etc.
> Easy Language Interoperability
Even easier: JSON!
And you don't have to learn yet another DSL, and compile those down into lots of boilerplate!
I'm not trying to say that you shouldn't use Protocol Buffers if its a good fit for your software, but this list is a bit anemic on real reasons to use them, particularly for dynamically typed languages.
- Arkadir 12y ago> structure = json.loads(json_string) Not quite. Let's say your JSON data contains the following attribute: "access" : [ "view", "edit", "admin" ], This field should be represented (in the language) as a set of values from an "access-levels" enumeration. In C#, you'd have the following boilerplate: [DataMember(Name = "access")] public HashSet<AccessLevels> Access { get; private set; } In OCaml, it would be: access : Access.Set.t ; The simple "json.loads" solution would return a list of strings instead. What's the Python code for turning it into a set of enumeration values, and failing if one of the values does not match ?
- falcolas 12y agoPython doesn't have enumerations, per se. Here's how I'd represent that: if 'edit' in structure['access']: # can edit In which case it really doesn't matter if there's junk in access. If I really felt the need to validate the structure, I could do so with: if not set(structure['access']).issubset(all_access_privs): raise ValueError('invalid access types passed in') but more realistically, I'd rely on the ORM object to validate against the authoritative source - the database - and key off the errors there.
- TomaszZielinski 12y agoPython 3.4 has enums, if that's what you meant: https://docs.python.org/3/library/enum.html https://docs.python.org/3/library/enum.html
- colanderman 12y agoBut that's not a problem with JSON-the-serialization; that just means JSON begs to be extended with a rich schema definition language. It's kind of like saying "language X sucks because it doesn't have an automatic build tool; use language Y instead". You just need a build tool for X, not a new language.
- PaulFreund 12y agohttp://json-schema.org/ http://json-schema.org/
- theseoafs 12y ago> No reason you can't implement schemas over JSON. In fact, you typically implicitly do Right, that's the point -- since in normal use you impose a set of "schema" requirements over all data interchange formats, even schemaless ones, it's a strictly good thing to have that schema explicitly written out. It means the compiler can verify your types are correct and the runtime can verify your messages have all the fields they'll need. > JSON is unversioned, so you can add and remove fields as you wish. Sure, but if you do, you have to handle the version-management code at the application level, manually, where it's really easy to make mistakes. > And for languages where type safety is in place, the JSON libraries frequently allow you to pre-define the data structure which the JSON will attempt to parse into, giving type safety & a well define schema for very little additional overhead as well. Sure, and if you're going to do that, you might as well use protobufs which is going to be much faster/more lightweight.
- falcolas 12y ago> Sure, and if you're going to do that, you might as well use protobufs which is going to be much faster/more lightweight. If you're looking for fast, you're probably not looking at an interpreted language. Plus, there are faster protocols out there than protobufs, if speed is all you care about. > It means the compiler can verify your types are correct and the runtime can verify your messages have all the fields they'll need. Again, the OP was referring to using protobufs in Ruby. Type safety and the compiler are not high priorities when writing in Ruby. > Sure, but if you do, you have to handle the version-management code at the application level, manually, where it's really easy to make mistakes. That depends on your application's requirements - for most cases, Python/Ruby/Javascript make it very easy to add or deprecate fields in dictionaries/hashes without having to explicitly worry about versions. Just specifying default values when requesting a key. And since you can ignore hash pairs with even less effort, deprecating portions of an API are simple. Protobuffers definitely has its place, I'm just not convinced that place is in dynamically typed languages like Python, Ruby or Javascript.