Object-c Programming tips-jastor automatic Parsing

Source: Internet
Author: User

Object-c Programming tips-jastor automatic Parsing
Preface

In the past, no automatic resolution was used for writing iphone programs, and all of them were manually resolved by the dictionary layer. This is time-consuming, labor-consuming, and error-prone. Later, the company came to a new friend and brought the automatically parsed jastor library, which was really good.

Brief Introduction to jastor

Jastor is a library based on the oc runtime. It can convert dictionary objects to NSObject objects. It supports NSString, NSNumber, NSArray, NSDictionary, and their nested types. For example, you need to convert dict to model. Dict is an NSDictionary object, and model is an NSObject object inherited from Jastor. Jastor traverses all attributes of the model object and uses the attribute name as the key of dict to obtain the dictionary value. Then, different processing is performed based on the value type. If the value is of the NSArray type, the NSArray parsing process is started. If the value is of the NSDictionary type, recursive calling is started. If the value is of another common type, setvalue: forKey is used directly. When you enter the NSArray processing process, call the "attribute _ class" method of the class to obtain the type of the array object, and then parse the array object step by step. The array object must also be of the dictionary type.

Why write this blog?

Jastor can greatly simplify the code parsing process. Of course, it is certainly not highly efficient to manually parse the Code. This can be seen from the recursive process of the Code and various traversal methods using Runtime. However, as a developer, the efficiency loss can lead to a lot of concise code and reduce the chance of errors. The benefits are obvious. When using Jastor for the first time, it attracted the conciseness of its code, mainly including recursive parsing ideas, rumtime-related content, combination mode, and cache mechanism. After being added to the Code, there will always be some crashes, mainly due to fault tolerance issues. After all, we have no way to require that the json data sent by the server always meet our requirements, there are always some inconsistencies that cause unexpected problems.

Jastor recursive Parsing

Jastor is a base class. It can convert a dictionary into a model. If a dictionary member is a dictionary, it can be recursive to continue parsing, it is not until a common single data type such as NSString and NSNumber is encountered. Through this layer-by-layer recursion, it goes deep into the bottom layer, that is, the end of Jastor parsing. It traverses all the attributes of the current class through the for loop and finds the value values of the attributes in the dictionary one by one. Based on the different types of value values and the type of attributes, it determines whether to perform recursive parsing next or directly setValueForKey.

If ([dictOrArray isKindOfClass: nsDictionaryClass]) {value = [dictOrArray valueForKey: jsonKey]; if (value = [NSNull null] | value = nil) {continue ;} if ([JastorRuntimeHelper isPropertyReadOnly: [self class] propertyName: classAttrName]) {continue;} // handle dictionary if ([value isKindOfClass: Attributes]) {Class klass = [JastorRuntimeHelper attributes: classAttrName o FClass: [self class]; if (! [Klass isSubclassOfClass: [Jastor class]) {NSLog (@ "the data type returned by the server is inconsistent with that returned by the client"); continue;} value = [[[klass alloc] initWithDictOrArray: value] autorelease];} // handle array else if ([value isKindOfClass: nsArrayClass]) {value = [self dealWithArrayValue: value attrName: classAttrName jsonKey: jsonKey];} // handle nsnumber else if ([value isKindOfClass: [NSNumber class]) {value = [(NSNumber *) value stringValue];}

Please refer to the above Code. When dict is passed in, it will enter this if process. First, judge whether the value is null. if it is blank, skip it directly; then we can judge whether the attribute is read-only and skip the attribute directly. Then, we can determine whether the value is a dictionary. If the attribute is a dictionary, we can perform recursive analysis. Then, we can determine whether the attribute is an array, the array is parsed through traversal of array elements, and the final result is NSNumber. The string value of NSNumber is obtained. After obtaining the final value, the following operations will be performed:

            // handle all others            if (value) {                [self setValue:value forKey:classAttrName];            }

Jastor Runtime

Runtime is better, but there are only a few methods in it. However, it may feel a little too high because it is rarely used. In fact, a closer look is just a normal property traversal. There are several methods in total: Determine whether the attribute is readable, obtain an array of all attribute lists of a class object, and determine the type of the given attribute of the specified class object. All methods are based on a basic idea: traversing all the properties of the Class Object objc_property_t for processing. The efficiency here must be relatively low, but the author uses the cache idea to traverse and write data into the cache without buffering, and read the cache directly next time.

Jastor Combination Mode

The model of a derived class inherits from Jastor, and a certain attribute of the derived class model may also inherit from Jastor. A large model is a combination of a bunch of Jastor sub-classes. This idea is similar to the combination mode and runs through recursive data parsing, but the common combination mode is not a combination of classes and attributes, in addition, the combination of a set and a single item has the same principle.

Jastor Cache Mechanism

You can see that there are various caches, including obtaining the attribute list, whether it is readable, getting the Class Object of the specified attribute, and getting the Class Object of the common class, with only a few simple dictionaries, the execution efficiency of the program can be greatly improved, which is quite good.

Jastor Fault Tolerance

Jastor has implemented various fault tolerance methods, but it still has some defects for applications. After all, the program can be faulty but cannot crash. This is a basic principle. Therefore, I found several failures through guesses and tests, corrected them with my colleagues, and added several fault tolerance points. At least in our applications, there was no crash.

Map features of Jastor

In the new version of Jastor, map is added, and the attribute name can be arbitrary, giving the program maximum flexibility. The old version we used didn't have this content. Our group's cool-man Bobo joined the map mechanism. At the beginning, I felt the necessity was not great, but with the development of later projects, it is indeed very useful.


For Jastor, I will not post the code. Github has a library and GitHub address. In case of a Jastor crash, you must follow up on Jastor, locate the problem, and add protection.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.