在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:SBJson/SBJson开源软件地址:https://github.com/SBJson/SBJson开源编程语言:Objective-C 98.5%开源软件介绍:SBJson 5Chunk-based JSON parsing and generation in Objective-C. OverviewSBJson's number one feature is stream/chunk-based operation. Feed the parser one or more chunks of UTF8-encoded data and it will call a block you provide with each root-level document or array. Or, optionally, for each top-level entry in each root-level array. With this you can reduce the apparent latency for each download/parse cycle of documents over a slow connection. You can start parsing and return chunks of the parsed document before the full document has downloaded. You can also parse massive documents bit by bit so you don't have to keep them all in memory. SBJson maps JSON types to Objective-C types in the following way:
"Plain" Chunk Based ParsingFirst define a simple block & an error handler. (These are just minimal examples. You should strive to do something better that makes sense in your application!) SBJson5ValueBlock block = ^(id v, BOOL *stop) {
BOOL isArray = [v isKindOfClass:[NSArray class]];
NSLog(@"Found: %@", isArray ? @"Array" : @"Object");
};
SBJson5ErrorBlock eh = ^(NSError* err) {
NSLog(@"OOPS: %@", err);
exit(1);
}; Then create a parser and add data to it: id parser = [SBJson5Parser parserWithBlock:block
errorHandler:eh];
id data = [@"[true," dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data]; // returns SBJson5ParserWaitingForData
// block is not called yet...
// ok, now we add another value and close the array
data = [@"false]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data]; // returns SBJson5ParserComplete
// the above -parse: method calls your block before returning. Alright! Now let's look at something slightly more interesting. Handling multiple documentsThis is useful for something like Twitter's feed, which gives you one JSON document per line. Here is an example of parsing many consequtive JSON documents, where your block will be called once for each document: id parser = [SBJson5Parser multiRootParserWithBlock:block
errorHandler:eh];
// Note that this input contains multiple top-level JSON documents
id data = [@"[]{}" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
[parser parse:data]; The above example will print:
Unwrapping a gigantic top-level arrayOften you won't have control over the input you're parsing, so can't use a multiRootParser. But, all is not lost: if you are parsing a long array you can get the same effect by using an unwrapRootArrayParser: id parser = [SBJson5Parser unwrapRootArrayParserWithBlock:block
errorHandler:eh];
// Note that this input contains A SINGLE top-level document
id data = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data]; Other features
A word of warningStream based parsing does mean that you lose some of the correctness verification you would have with a parser that considered the entire input before returning an answer. It is technically possible to have some parts of a document returned as if they were correct but then encounter an error in a later part of the document. You should keep this in mind when considering whether it would suit your application. American Fuzzy LopI've run AFL on the sbjson binary for over 24 hours, with no crashes found. (I cannot reproduce the hangs reported when attempting to parse them manually.)
API DocumentationPlease see the API Documentation for more details. InstallationCocoaPodsThe preferred way to use SBJson is by using CocoaPods. In your Podfile use:
CarthageSBJson is compatible with Carthage. Follow the Getting Started Guide for iOS.
Bundle the source filesAn alternative that I no longer recommend is to copy all the source files (the
contents of the Examples
Support
Philosophy on backwards compatibilitySBJson practice Semantic Versioning, which means we do not break the API in major releases. If something requires a backwards-incompatible change, we release a new major version. (Hence why a library of less than 1k lines has more major versions than Emacs.) I also try support a gradual migration from one major version to the
other by allowing the last three major versions to co-exist in the
same app without conflicts. The way to do this is putting the major
version number in all the library's symbols and file names. So if v6
ever comes out, the LicenseBSD. See LICENSE for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论