Giter Club home page Giter Club logo

nsjsonserialization-comments's Introduction

NSJSONSerialization+Comments

This Objective-C category adds C style comment support to NSJSONSerialization. It allows you to read JSON files with // and /* โ€ฆ */ comments. It is similar to JSONKit's JKParseOptionComments option.

Usage

You can use this category's JSONObjectWithCommentedData:options:error: method just like NSJSONSerialization's JSONObjectWithData:options:error::

id object = [NSJSONSerialization JSONObjectWithCommentedData:data options:0 error:&error];

It strips single line and multi-line comments as well as whitespace before it hands over the data to NSJSONSerialization.

The code works directly on UTF-8 data without converting it to an NSString, but also detects UTF-16 and UTF-32 byte order marks (BOM). If a non-UTF-8 BOM is detected, it converts the data to UTF-8. The most efficient encoding to use for parsing is UTF-8.

I've also added the convenience methods JSONObjectWithCommentedContentsOfURL:options:error: and JSONObjectWithCommentedContentsOfFile:options:error: to load JSON directly from a file or url.

Finally stringWithJSONObject:options:error: is a convenience method around dataWithJSONObject:options:error: if you need an NSString instead of NSData.

Have a look at this blog post to read more about the motivation behind NSJSONSerialization+Comments.

ARC Support

If you are including NSJSONSerialization+Comments in a project that has Automatic Reference Counting (ARC) enabled, you will need to set the -fno-objc-arc compiler flag. To do this in Xcode, go to your active target and select the "Build Phases" tab and open the "Compile Sources" section. In the "Compiler Flags" column, set -fno-objc-arc for NSJSONSerialization+Comments.m.

License

Code in this repository is licensed under the MIT license (see the LICENSE file).

nsjsonserialization-comments's People

Contributors

blach avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.