Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a basic JSON parsing class

Tags:

java

json

parsing

Could some one guide how to write a class which would take a JSON data and would try to parse it into a simple buffered list from which we could read the data back?

Ex. JSON

{ name: 'John', age: 56 } 

..will be parsed into a table of key value pairs

name John
age  56

How to write a parse method which would help to create a faster and simpler?

Kindly do not suggest any existing library. Provide a concept for parsing JSON.

like image 921
Samual krish Avatar asked Jun 12 '13 10:06

Samual krish


People also ask

What is JSON parsing example?

Example - Parsing JSON Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How do you write JSON correctly?

JSON - Syntax Data is represented in name/value pairs. Curly braces hold objects and each name is followed by ':'(colon), the name/value pairs are separated by , (comma). Square brackets hold arrays and values are separated by ,(comma).

How do I parse a JSON file?

If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method. If you need to parse a JSON file that returns a dictionary, then you can use the json. load() method.


1 Answers

This answer assumes that you really want to write a parser and are prepared to put in the effort required.

You MUST start with the formal specification of JSON. I have found http://www.ietf.org/rfc/rfc4627.txt. This defines the language precisely. You MUST implement everything in the spec and write tests for it. Your parser MUST cater for incorrect JSON (like yours) and throw Exceptions.

If you wish to write a parser, stop, think and then don't. It's a lot of work to get it working correctly. Whatever you do, make a proper job of it - incomplete parsers are a menace and should never be distributed.

You MUST write code that conforms. Here are some phrases from the spec. If you don't understand them you will have to research carefully and make sure you understand:

"JSON text SHALL be encoded in Unicode. The default encoding is UTF-8."

"A JSON parser MUST accept all texts that conform to the JSON grammar."

"Encoding considerations: 8bit if UTF-8; binary if UTF-16 or UTF-32

  JSON may be represented using UTF-8, UTF-16, or UTF-32.  When JSON
  is written in UTF-8, JSON is 8bit compatible.  When JSON is
  written in UTF-16 or UTF-32, the binary content-transfer-encoding
  must be used.

"

"Any character may be escaped. If the character is in the Basic
Multilingual Plane (U+0000 through U+FFFF), then it may be
represented as a six-character sequence: a reverse solidus, followed
by the lowercase letter u, followed by four hexadecimal digits that
encode the character's code point. The hexadecimal letters A though
F can be upper or lowercase. So, for example, a string containing
only a single reverse solidus character may be represented as
"\u005C". "

If you understand these and still want to write a parser, then review some other parsers, and see if any of them have conformance tests. Borrow these for your own application.

If you are still keen you should strongly consider using a parser generator. Examples are JAVACC, CUP and my preferred tool, ANTLR. ANTLR is very powerful but can be difficult to start with. See also the suggestion of Parboiled, which I would now recommend. JSON is relatively simple and it would be a useful exercise. Most parser-generators generate a complete parser which can create executable code or generate the parse tree of your JSON.

There is a JSON parser-generator using ANTLR at http://www.antlr.org/wiki/display/ANTLR3/JSON+Interpreter if you are allowed to peek at it. I have also just discovered a Parboiled parser-generator for JSON. If your main reason for writing a parser is learning how to do it, this is probably a good starting point.

If you are not allowed (or don't want to) use a parser-generator then you will have to create your own parser. This generally comes in two parts:

a lexer/tokenizer. This recognizes the basic primitives defined in the language spec. In this case it would have to recognize curly-brackets, quotes, etc. It would probably also build the representation of numbers.

an AbstractSyntaxTree (http://en.wikipedia.org/wiki/Abstract_syntax_tree, AST) generator. Here you write code to assemble a tree representing the abstraction of your JSON (e.g. whitespace and curlies have been discarded).

When you have the AST it should be easy to iterate over the nodes and create your desired output.

But writing parser-generators, even for a simple language like JSON, is a lot-of-work.

like image 100
peter.murray.rust Avatar answered Oct 18 '22 22:10

peter.murray.rust