开发者

json schema to objective c - model classes generation

开发者 https://www.devze.com 2023-02-16 06:53 出处:网络
Hi stack overflow people i am writing an iphone app thats connects to a web service, through REST with JSON.

Hi stack overflow people

i am writing an iphone app thats connects to a web service, through REST with JSON. I would like to generate my model classes in objective c from a json schema provided by my web service, a bit like i d do with wsdl开发者_如何学编程2objc with asoap, xml and wsdl combination.

It looks like there s not much out there on this subject

i tried something called jsonschema2objc.rb from http://code.google.com/p/bkjsonschema/ but it s giving me errors even on the simplest json schema, the one found here: http://en.wikipedia.org/wiki/JSON#Schema

i get this error:

Using temporary file /var/folders/rN/rNw33pkyHVeNG+-IesdU+k+++TI/-Tmp-/jsonschema2objc.8WRkBSQo !!! Object definition at index Product has unknown type

so here are my 2 questions:

  • do you guys know any good tool to acheive jsonchema => objective c classes ?
  • do you know what this error means in my ./jsonschema2objc.rb

Thx!


There was a very nice tool in the Mac Appstore called JSONModeler (now it's discontinued) but the same guys make the JSON Accelerator which is an evolution of the same program. It's really good (and cheap) and it generates Objective-C, Java and Python code (the JSONModeler also supported Coredata classes and schema, I don't know if they still support it in the new one though).


I'm the developer of jsonschema2objc, it relies on a superset of JSON schema. That is, we needed to tweak a bit JSON schema to add additional properties that specify the mapping between JSON and Obj-C. For instance, you can specify a different property name in the JSON and the generated Obj-C class. We already needed to add the notion of Type Converters and Type Resolvers those are needed by the generated parsing code to convert raw strings to dates (NSDate) objects for instance or to resolve the actual subclass of a parsed object. We support one-to-many relationships containing objects that are subclasses of a given class. At runtime we will use the additional metadata provided in the schema to figure out which class to instanciate.

I realize that we put this script this out there without any sample schemas to better understand how to use it. I will solve that in the next few days...

In the meantime, I recommend you use the TextMate bundle that comes with the project. We support dozens of TextMate snippets that automates to process of creating a valid schema.


It's not free, but perhaps Objectify would do what you want? It seems very slick.


https://github.com/BrunoAlexandreMendesMartins/CleverModels

Given a certain URL, CleverModels will make the request for you and generate the models according to the JSON response.

It's a new tool and open source.

The goal is to support multiple languages.


You can try json2objc. One of my friend developed this for himself and after my insists he made it public service. It's not commercial.


Check out Nidyx! It's got JSON Schema to Obj-C model support (in the form of a CLI, not a website), it can spit out JSONModel conforming classes, and it's got Swift support and potentially Java coming soon!


I like to use ESJsonFormat. This allows me to paste my JSON into a textbox and the plugin makes the Classes.

E: as of xcode 8, plugins are not 'officially supported'. See here


There is a tool called "JSON Class Generator" available that "generates Objective-C source code from the classes and enums that you define in this editor". Also you can also find it in the Mac App Store.

It does NOT have an import functionality for jsonschema, but does generate pretty complete models from what you define in the gui.

Maybe it is an option for you to re-enter all the models in the editor. If you go for a library you certainly have more work to do. Using the App you can get the benefits you desire from code generation. (my personal reasons would be: less work, less error prone, less runtime checks, easier modification, more features of the generated code).

The App is free.

Since json schema is not directly supported: there might be the option to create a converter. I checked the document file format of the above mentioned App. It is also JSON and it looks straight forward (kind of similar to json schema). So a converter could be possible to a certain degree. But I fear, that the problem lies in the details - like so often - and that a "I write a converter in a day" becomes a "I am writing a converter, it took me 2 weeks so far and I am still not finished". It would be a cool open source project to start...


If you can't find a third-party tool you can get close with the idea below.

JSON gives you back a dictionary similar to loading a plist? You could implement wrapper class that holds the dictionary and overrides two methods of NSObject:

   -(BOOL) respondsToSelector:(SEL)selector
   -(id) performSelector:(SEL)selector

If the selector's name is in the dictionary it responds to the selector and when performing the selector it returns the object, if that object is a dictionary it wraps that dictionary in the wrapper class and return that.

Now this will not allow you have good type information, but you'll be able to drill down the dictionary like this:

NSValue *universalGravity = (NSValue*) [[[jsonResponds settings] universal] gravity];

Just an idea I had when reading your question and thought it could be a fast, good enough solution if you can't find some third-party that's already implemented what you're looking for.


Pre-writing model code is not the most flexible solution. You can have clever models, which try to convert your incoming JSON to your defined properties. Have a look at the JSONModel Obj-C framework.

It has tons of demos and tests included and it's very easy to write models with it:

https://github.com/icanzilb/JSONModel


hi you can use this tool to get json to obj c Object.

http://www.realmgenerator.eu/


You can use the link below:

Convertor Link - This utility generates a Swift 2.0 compatible models which can be simply dragged & used in your project

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号