/SwiftJSONSolution

Provide an efficient solution to convert objects between Swift objects(struct) and JSON

SwiftJSONSolution

Provide an efficient solution to convert objects between Swift objects(struct) and JSON.


Table of contents




1. JSON to Swift Solution

TODO:
  • load local JSON file
  • display JSON loaded from URL using "pretty" format
  • add a feature: "add properties that doesn't exist in JSON file"
  • deal with situation where a arry contains different objects
  • enhance the function "loading JSON via url"
  • flash JSON objects when editting
  • add code highlight (low priority for now)

Since Swift doesn't support dynamicly mapping JSON to struct (as explained in this Swift blog), the fastest way to generate model from JSON seems to be generating the code and initialisers with JSON properties.

The app JSON2Swift is a straightforward and easy to use tool to generate swift models. It also supports full customization of a model and properties when it is required.

Here is the tutoral for JSON2Swift:

  1. Download the app from macOS App Store
  2. Copy & Paste JSON content to the input text field, or enter the URL for JSON and load it. (Note: the app currently only support JSON whose root is a dictionay structure.) Screenshot
  3. Enter the meaningful name of the model to replace the generated model name. Screenshot
  4. Click the export button. Optionally you can enter the information of your project to generate the header of swift file. Screenshot

If there is any problem regarding using the software, please raise an issue here.

2. Swift to JSON Solution

Luckily, by implementing reflection using Mirror, it is feasible to implement this feature in the code. Please see the details in another repository YHJSONGenerator