Json to Model code tool~

Json to Model code tool~

JsonMagic is a tool for converting Json to Model

v1.1.6 Update!

Hi Dapu Ben 2021-02-06 v1.1.6 released!

content:

  1. Added Json to Objective-C function;
  2. Refactor the output part to make custom output easier;

Preface

JsonMagic is an application for converting Json data into class definition code on Mac. There are many types of converted codes, including Swift, Kotlin, and Java. It also supports converting Kotlin Model to Swift Model. It is suitable for clients and Java back-end engineers. Of course, if you customize it, it can be used as long as there are development requirements for Modeling.

The project is written by an individual in Swift and has been open source. Interested coders can fork and change it into a tool that suits them. Click me for the code address . There is a DMG file inside, which can be downloaded and used directly.

The outline of the article is as follows:

  1. Application background;
  2. Application characteristics;
  3. Use introduction;
  4. Code framework & user-defined output;
  5. Conclusion

Application background

The biggest original intention of the application is to reduce the repetitive and low-tech work time of converting Json to Model in software development, and improve the efficiency of development work. The wages of programmers are not low in all industries, and working time is precious. The extra time can be used to learn technology and improve ourselves, or just watch the NBA or visit station B. The mood is also very good.

The purpose of Json modelling is to make the data easy to read and easy to process. It is very common in application development, and the most common scenario is to convert server-side data into a Model class that is easy to use on the client-side. The conversion content is mainly two parts, one is to convert the dictionary node into a class, and the second is to convert the key value in the dictionary into the field name of the class, and the type of the field is the value type corresponding to the key.

This process is very mechanized and can be automatically converted by program. There are not many tools in this area on the iOS platform, and most of them are command-line tools, which are not friendly enough for students who are used to interface development. The Android platform has many useful tools, the best experience is the Android Studio plug-in. The process of converting Json to Model can be completed in Android Studio.

Why do you have to make new tools yourself?

Summary: 1. for iOS, there are no friendly tools with an interface; secondly, all the tools on the iOS and Android platforms are not good enough for the organization and fault tolerance of Json data. Mainly reflected in commas, comments:

  1. comma. The comma after the last object in the array, or the comma of the last property in the object, will be reported by the tools currently on the market. For example: the following example cannot be generated normally

    {
    	"name": "zhang san",
    	"address_list ": [
    		{
    			"code": 12345,
    			"receiver_name": "li si",
    			"receiver_address": " ",
    		},
    	],
    	"city_code": "010",
    }
     

    Where illegal commas appear:

    1. of the first element of address_list},
    2. "receiver_address": "Haidian District, Beijing",
    3. "city_code": "010",
  2. Comment. JSON data is sometimes added with some comments to make it easier to read, and the current tools on the market will report errors when parsing them. For example: the following example cannot be generated normally

    {
    	"name": "zhang san",
    	"address_list ": [
    		{
    		  // 
    			"code": 12345,
    			"receiver_name": "li si", // 
    			"receiver_address": " " // 
    		}
    	],
    	"city_code": "010" // 
    }
     

And JsonMagic is to solve these two problems.

Application characteristics

The characteristics of JsonMagic are summarized as follows:

  1. Common illegal comma fault tolerance;
  2. Specification annotation fault tolerance;
  3. Unified iOS & Android. Integrate conversion work between iOS and Android platforms. Support to convert Swift code, Kotlin code, Java code. It also supports the conversion of Kotlin Model to Swift Model;
  4. Custom conversion capabilities. The code is open source and JSON is abstracted. Developers only need to care about the final conversion part;
  5. Kotlin optional annotations. You can check whether SerializedNameor not JsonProperty;

Use introduction

After the application is started, the interface is as shown below:

In the larger area in the middle, the left side is the Json data entered by the user, and the right side is the generated Model data.

The other operation parts are divided into five parts, according to the target numbers:

  1. Select the code to be converted, the default is Swift. Json to Swift, Json to Kotlin...;
  2. You can specify the suffix, the default is Model; you need to fill in the name of the Model, without the suffix;
  3. Click Run to start generation, and the generated code is in the Model box. The status of success and failure will be displayed at the bottom;
  4. Operation on the result. For example, Copy Modelcopy to the clipboard. The latter is the generated file;
  5. If you want to see the analysis process, you can click the Log button, and there will be a pop-up window;

Success status icon:

Failure to fill in the Model name icon:

There are extra comments, commas but the parsing success status icon:

Code Architecture & Custom Modeling

Jsonized code is mainly divided into three processes, as shown in the figure:

  • First preprocess Json, check necessary input, remove comments, etc.;

  • Secondly convert Json custom objects, using Jsonic.DataTypepreservation;

    internal indirect enum DataType: Equatable {
            static func == (lhs: Jsonic.DataType, rhs: Jsonic.DataType) -> Bool {
                return lhs.swiftDescription == rhs.swiftDescription
            }
            
            case string, int, long, double, bool, unknown
            case array(itemType: DataType)
            case object(name: String, properties: [PropertyDefine])
            ...
     
  • Finally, according to different generation types, generate different output codes;

    public enum OutputType {
        public struct KotlinConfig {
            ///should output SerializedName
            var isSerializedNameEnable: Bool
            ///should output JsonProperty
            var isJsonPropertyEnable: Bool
        }
        
        case swift
        case kotlin(config: KotlinConfig)
        case java
        ...
     

Custom output

From the perspective of the overall architecture, custom output only needs to care about the last step of Output. Only two steps are required:

  1. Add an OutputType type;
  2. Define class and property output;

Is it easier than putting the elephant in the refrigerator~

Take Swift as an example: The first step is to add Swift type:

public enum OutputType {
    ...
    case swift
    case kotlin(config: KotlinConfig)
    case java
    ...
 

The second step is to add custom model conversion code (new SwiftOutputclasses and inherit Modelableinterfaces):

class SwiftOutput: Modelable {
    typealias ModelConfig = DefaultModelConfig
    
    func modelDescription(name: String, properties: [Jsonic.PropertyDefine], config: DefaultModelConfig?) -> String {
        var text = "class \(name): Codable {\n"
        for property in properties {
            let typeDescription = dataTypeDescription(type: property.type)
            text += "    var \(property.name): \(typeDescription)?\n"
        }
        text += "}"
        return text
    }
    
    func dataTypeDescription(type: Jsonic.DataType) -> String {
        switch type {
        case .string:
            return "String"
        case .int:
            return "Int"
        case .long:
            return "Int64"
        case .double:
            return "Double"
        case .bool:
            return "Bool"
        case .unknown:
            return "String"
        case .object(let name, _):
            return name
        case .array(let itemType):
            let typeDescription = dataTypeDescription(type: itemType)
            return "Array<" + typeDescription + ">"
        }
    }
}
 

Modelable interface

Modelable The interface defines two methods that need to be implemented for personalized output:

protocol Modelable {
    associatedtype ModelConfig
    func modelDescription(name: String, properties: [Jsonic.PropertyDefine], config: ModelConfig?) -> String
    func dataTypeDescription(type: Jsonic.DataType) -> String
}
 

modelDescription Define how to format the output Model;

dataTypeDescription Define the formatted output of each basic or advanced type;

ModelConfig It is a customized personalized attribute configuration in the interface, currently only available in Kotlin conversion;

At last

A few years ago, I also used the command line (Python) to convert the Model. Recently, I just turned it into an interface and rewritten it with Swift code. There may be bugs in the code, so please leave a message and I will fix it as soon as possible. Of course, code friends can also upgrade and change by themselves after fork, but I still hope that you can feedback if you have any questions, so that you can help more students. Happy Coding~