I need to convert json to objective-c object. I mean not parse it in code, but generate the code of the object by some json. Are there some tools for it? I know how to do it in java(jsonpojo).
Also i wonder if there is a certain way to automatically store data from json to a real objective-c Object without manual parsing.
In Java its done by Gson library.
It looks like you are describing exactly what ROAD iOS Framework. Take a look: https://github.com/epam/road-ios-framework
The tool for generating plain model classes from JSON is ROADClassesGenerator and if you will use ROAD for networking then serialization into generated classes comes for free.
One note, it's not actively supported now, due to pervasiveness of Swift and the fact if its tight reliance on macros which are still poorly supported in Swift, but it's still work and do the job nevertheless.
Related
I'm currently struggling with the experimental KXS-properties serialization backend, mainly because of two reasons:
I can't find any documentation for it (I think there is none)
KXS-properties only includes a serializer / deserializer, but no encoder / decoder
The endpoint provided by the framework is essentially Map<String, Any>, but the map is flat and the keys already have the usual dot-separated properties syntax. So the step that I have to take is to encode the map to a single string that is printable to a .properties file AND decode a single string from a .properties file into the map. I'm generally following the Properties Format Spec from https://docs.oracle.com/javase/10/docs/api/java/util/Properties.html#load(java.io.Reader), it's not as easy as one might think.
The problem is that I can't use java.util.Properties right away because KXS is multiplatform and it would kinda kill the purpose of it when I'd restrict it to JVM because I use java.util.Properties. If I were to use it, the solution would be pretty simple, like this: https://gist.github.com/RaphaelTarita/748e02c06574b20c25ab96c87235096d
So I'm trying to implement my own encoder / decoder, following the rough structure of kotlinx.serialization.json.Json.kt. Although it's pretty tedious, it went well so far, but now I've stumbled upon a new problem:
As far as I know (I am not sure because there is no documentation), the map only contains primitives (or primitive-equivalents, as Kotlin does not really have primitives). I suspect this because when you write your own KSerializers for the KXS frontend, you can specify to encode to any primitive by invoking the encodeXXX() functions of the Encoder interface. Now the problem is: When I try to decode to the map that should contain primitives, how do I even know which primitives are expected by the model class?
I've once written my own serializer / deserializer in Java to learn about the topic, but in that implementation, the backend was a lot more tightly coupled to the frontend, so that I could query the expected primitive type from the model class in the backend. But in my situation, I don't have access to the model class and I have no clue how to retrieve the expected types.
As you can see, I've tried multiple approaches, but none of them worked right away. If you can help me to get any of these to work, that would be very much appreciated
Thank you!
The way it works in kotlinx.serialization is that there are serializers that describe classes and structures etc. as well as code that writes/read properties as well as the struct. It is then the job of the format to map those operations to/from a data format.
The intended purpose of kotlinx.serialization.Properties is to support serializing a Kotlin class to/from a java.util.Properties like structure. It is fairly simple in setup in that every nested property is serialized by prepending the property name to the name (the dotted properties syntax).
Unfortunately it is indeed the case that this deserializing from this format requires knowing the types expected. It doesn't just read from string. However, it is possible to determine the structure. You can use the descriptor property of the serializer to introspect the expectations.
From my perspective this format is a bit more simple than it should be. It is a good example of a custom format though. A key distinction between formats is whether they are intended to just provide a storage format, or whether the output is intended (be able to) to represent a well designed api. The latter ones need to be more complex.
I'm trying to write/read a class object from/to a file.
I'm new to D and I just want to play a little bit around with it.
Is there a Class/Function to write/read an object to/from a file?
I'm looking for something similar to the ObjectOutputStream сlass in Java.
Or do I have to serialize (concatenate) the object's variables as strings in the file?
I have a Movie class and a MovieManager class, which contains a dynamic movie-array.
A Movie object contains just a few strings and integer values.
Extending answer, provided in comment, it is worth explicitly stating, that D does not provide "one true way" of reading/writing objects to/from files, as there can't be a single optimal one. Different considerations about speed, resulting file format, handling references and similar corner cases may results in different serialization strategies.
That being said, most likely proper serialization library is needed, and, by lucky chance, one of most mature D solutions ("Orange" by Jacob Carlborg https://github.com/jacob-carlborg/orange) is being reviewed right now as a candidate for inclusion into standard library as a std.serialization: newsgroup thread. It may be your best bet.
The library Unmanaged provides a serialization system. You also have Orange
which is less restrictive, as Unmanaged serialization only works if the object to serialize is an ancestor of one of the framework base class.But...Unmanaged works on the "accessor" principle. The data serialized are get via a method and the data deserialized are set via a method, which allows to update some stuffs when the deserializer recall for example...
I can easily find tools that:
transform JSON to csharp model classes
transform JSON to objc model classes
transform JSON to POJOs for java
etc.
But what happens if I'm looking to step in the middle of this process and roll my own transformer from JSON to blah model classes?
Is there a programming framework or tool that understands JSON very well and simply provides hooks or callbacks that I can programmatically implement, in order to do the second half of the job, which is to spit out my own style of model classes?
My motivation behind this question is the fact that I found: http://jsonpack.com/ModelGenerators/ObjectiveC which helps generate ObjC model classes which are dependent on the JSONKit framework but I would like to generate ObjC model classes that are dependent on the RestKit framework instead.
If I understand you correctly, you are probably looking for a "SAX style" parser. That is, you implement some sort of a delegate for a parser which receives parse events from the parser and which handles these events appropriately.
So, instead of creating a JSON representation which is a hierarchy of Foundation objects consisting of NSArray, NSDictionary, and NSString etc., your delegate instead creates one or more instances of certain custom classes.
While this is feasible, if you have such a "SAX style" parser (which NSJSONSerialization is not), it certainly requires some amount of effort. Note, that you can always "convert" a Foundation hierarchy to any other object - say a Core Data model object. (Means, initialize an object from a NSDictionary). But, the more elaborate approach is certainly faster.
The two Objective-C JSON parser frameworks that I know of which have a SAX style API are SBJson https://github.com/stig/json-framework/ and JPJson https://github.com/couchdeveloper/JPJson.
The latter parser library (JPJson) is in fact quite obviously designed by separating these two tasks: a) parsing and b) performing semantic actions. You can subclass from an existing base class "semantic actions" "and create your own and override "handler methods" which correspond to handle the parse events.
I'm the author of JPJson and currently updating it to accommodate for new clang compiler (C++11) and Xcode.
I am looking to create / instantiate the levels and level enemies of a game I'm creating from an xml file at runtime that I will initially edit by hand until I have time to create a level editor. Once I have time to make the editor, I wish to automatically serialize and save my levels to disk. My enemy class uses CGPoints, floats and enums, along with basic datatypes like NSString.
Being new to objective-c, I am having trouble working out the most efficient way of doing this. NSPropertyListSerialization appears to only support basic data types and from reading the docs, it looks as though NSCoder serialises / deserializes using its own format.
Any advice is appreciated.
This library enables you to save any object to a property list and this one to JSON without making any additional effort. If you're wondering how they work, have a look at the Archives and Serialization Programming Guide.
I've recently reread the interesting tutorial from Mike Ash about How to create classes at Objective-C Runtime
I has been a long time I am wondering where to apply this powerful feature of the language. I always see an overkill solution to most of the ideas that come to my mind, and I eventually proceed with NSDictionary. What are your cases of use of creating classes at runtime? The only one I see is an Obj-C interpreter... More ideas?
There's some possible options I see, when someone need to create class in runtime
To hide information about it (It won't help in most cases, but... you can)
To perform multiple-inheritance (If you really need it :)
Using your own language(i.e. some XML-like), that can be interpreted by your program, writted in Obj-C (Something like NSProxy, but even better.)
Creating some Dynamic-Class that can change it's behavior in runtime
In general.. There is some possible usages of this. But in real world, in default service applications there's no need to do this, actually:)
It could be used for example along Core Data or any API related to a database to create new classes of objects unknown at compilation time. However, I doubt this is used often, it's mostly the mechanism the system uses itself when it runs a program...
KVO, in the Cocoa frameworks, is implemented by dynamically creating "notifying" versions of your classes. See http://www.mikeash.com/pyblog/friday-qa-2009-01-23.html