Posts

Showing posts from September, 2023

Generate Model schemas from JSON and JSON Schemas in 20 languages using quicktype.io

Image
Sometimes you just want to create Model objects around some type of JSON payload of schema definition.  It seems like you either end up building your own tool, doing the conversion manually, or hunting around to find some schema generator.  Sometimes there just isn't a tool that you want to use and sometimes there is. I'm working Dart where we want to create immutable objects from deserialized JSON schema for AdaptiveCards.  I ran across an open-source tool, quicktype.io, that can generate JSON serializable model classes for over 20 different languages. There are several different settings that let do some customization.  You can then consume those classes/functions as is  or tune them to your needs. It is a great learning aid even if you don't use it for your models.  I really like how it lets me compare how different languages handle models and JSON. Disclaimer:  I have no idea who created https://quicktype.io or how it is run.  Pull down their GitHub repo and scan it

Creating Immutable Functional Style Dart Model Objects based on JSON or JSON schemas

Image
Flutter is opinionated about using a functional programming model that includes immutable data objects.  I've found that we tend to ignore that whole immutable thing when dealing with JSON data.   Projects can be loose when handling incoming JSON data and how it is accessed, sprinkling string-style map keys across the codebase.  We can reduce that tendency by providing models to eliminate the string keys and mutable models to stop people from directly manipulating maps of strings. Some languages are functional only.  Other languages have no way of generating immutable objects. In those cases, we just end up with objects that can be easily converted into and out of JSON structures and strings. I'm working in Dart right now so the rest of this will be Dart samples Modeling Longitude and Latitude Using the JSON Schema Let's pick something simple as an example.  We're going to use this Latitude and Longitude  JSON Schema  that follows the schema definition style from  http: