Generate data model code and JSON-parser code from JSON-files. Currently you can generate Swift and Objective-C code.
Features
Scans the whole JSON-file to get most information out of it
Detects variadic types within arrays, even in sub structures
Detects nullability attributes, e.g. when a single occurrence of a property is null or a property is missing, the property is declared as Optional/Nullable
Generates parsing instructions for Swift and Objective-C
Converts any JSON data-structure into a simple schema which then can be modified/adjusted and be used to generate Swift/Objective-C code
Usage
Prerequisites
Swift 5.0
Any Swift 5.0 compatible platform (e.g. macOS or Linux)
Build
swift build --configuration release
By default, you’ll find the executable in .build/release/ReverseJson
Test
swift test
General usage:
Usage: ReverseJson (swift|objc|export) NAME FILE <options>
e.g. ReverseJson swift User testModel.json <options>
Options:
-v, --verbose Print result instead of creating files
-o, --out <dir> Output directory (default is current directory)
-c, --class (Swift) Use classes instead of structs for objects
-ca, --contiguousarray (Swift) Use ContiguousArray for lists
-pt, --publictypes (Swift) Make type declarations public instead of internal
-pf, --publicfields (Swift) Make field declarations public instead of internal
-n, --nullable (Swift and Objective-C) Make all field declarations optional (nullable in Objective-C)
-m, --mutable (Swift and Objective-C) All object fields are mutable (var instead of
let in Swift and 'readwrite' instead of 'readonly' in Objective-C)
-a, --atomic (Objective-C) Make properties 'atomic'
-p, --prefix <prefix> (Objective-C) Class-prefix to use for type declarations
-r, --reversemapping (Objective-C) Create method for reverse mapping (toJson)
Sometimes the inferred model requires some small adjustments, e.g. a field which has been inferred as non-optional should be optional.
Therefore ReverseJson allows to export a simple schema which then can be adjusted and be used to generate Swift or Objective-C code:
Export ReverseJson schema
./ReverseJson export User testModel.json
The example from above produces the following schema:
If there is only a “type” property, it is also possible to simply use the type identifier, e.g. the following expressions
"string"
is identical to
{
"type": "string"
}
If the property “isOptional” is missing, the type is assumed to be non-optional. Instead of having a property “isOptional”, it is also possible to just add a “?” as a suffix to the type name. E.g. this expression
"string?"
is identical to
{
"type": "string",
"isOptional": true
}
Objects
Objects have an additional property “properties” which is a JSON-object containing all properties, where the key is the property name and the value is a schema. Optionally you can add the “name” property to override the auto-generated name when converting the model to code. E.g.
The type “any” allows to support multiple value types at the same place. To describe which types are allowed, use the “of” property. Optionally you can add the “name” property to override the auto-generated name when converting the model to code. E.g.
ReverseJson
Introduction
Generate data model code and JSON-parser code from JSON-files. Currently you can generate Swift and Objective-C code.
Features
Usage
Prerequisites
Build
By default, you’ll find the executable in
.build/release/ReverseJson
Test
General usage:
To create a Swift data model:
To create an Objective-C data model:
To export a ReverseJson schema:
Demo
Turns this:
…into this:
…or into this…
ReverseJson Schema
Sometimes the inferred model requires some small adjustments, e.g. a field which has been inferred as non-optional should be optional. Therefore ReverseJson allows to export a simple schema which then can be adjusted and be used to generate Swift or Objective-C code:
Export ReverseJson schema
The example from above produces the following schema:
Convert ReverseJson schema into Swift
General schema structure
A schema is a simple type description which always has the following structure:
If there is only a “type” property, it is also possible to simply use the type identifier, e.g. the following expressions
is identical to
If the property “isOptional” is missing, the type is assumed to be non-optional. Instead of having a property “isOptional”, it is also possible to just add a “?” as a suffix to the type name. E.g. this expression
is identical to
Objects
Objects have an additional property “properties” which is a JSON-object containing all properties, where the key is the property name and the value is a schema. Optionally you can add the “name” property to override the auto-generated name when converting the model to code. E.g.
Lists
Lists describe their content-type using the “content” property. E.g.
Any
The type “any” allows to support multiple value types at the same place. To describe which types are allowed, use the “of” property. Optionally you can add the “name” property to override the auto-generated name when converting the model to code. E.g.