Pages

Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Json parsing, Scala way



Most java developers are familiar with json parsing and object mapping using Jackson library's object mapper functionality that enables serializing POJOs to json string and back. In scala, using the play's json inception mechanism provides a subtle way to serialize json. Using the powerful Scala macros, (a macro is a piece of Scala code, executed at compile-time, which manipulates and modifies the AST of a Scala compile-time metaprogramming), it is able to introspect code at compile-time based on Scala reflection API, access all imports, implicits in the current compile context and generate code. This means the case classes are automatically serialized to json. Also, you can explicitly provide the path to json key and map the value to object's field. But, for simple case classes they are just another boiler-plate code. Use it when we need more powerful mapping and logic for serialized fields. So how does this mapping works? The compiler will inject code into compiled scala AST (Absract Syntax Tree) as the macro-compiler replaces, say, Json.reads[T] by injecting into compile chain and eventually writes out the code for mapping fields in json to object. Internally, play's json module use Jackson's object mapper (ref: play.api.libs.json.jackson.JacksonJson). 

You can add dependency in build.sbt in a minimal-scala project which will provide Json APIs from play framework -
  "com.typesafe.play" %% "play-ws" % "2.4.2" withSources()

For eg, if we have to two classes (in this case class),

case class Region(name: String, state: Option[String])
case class Sales(count: Int, region: Region)

You have to add the implicit  methods for reading and writing to and from json and objects. The methods marked implicit will be inserted for you by the compiler and type is inferred from the context. Any compilation will fail if no implicit value of the right type is available in scope.

implicit val readRegion = Json.reads[Region]
implicit val readSales = Json.reads[Sales]
implicit val writeRegion = Json.writes[Region]
implicit val writeSales = Json.writes[Sales]

If you interchange the order, from readRegion and readSales, you will get compilation error.As the compiler creates a Reads[T] by resolving case class fields & required implicits at COMPILE-time, If any missing implicit is discovered, compiler will break with corresponding error.

 Error:(12, 38) No implicit format for test.Region available.
   implicit val readSales = Json.reads[Sales]
 

Interesting method to try is the validate() method while converting json to object which will help to pin point the path of error.

Executing the following program:



Results:

This is testing json..
Test 1
-------
Result:Some(Sales(123,Region(West,None)))
Test 2
-------
Error at JsPath: /region/name
error.path.missing
()
Result:None
Test 3
------
Error at JsPath: /count
error.expected.jsnumber
Error at JsPath: /region/name
error.expected.jsstring
()
Result:None
Test 4
------
Result:{"count":123,"region":{"name":"West","state":"California"}}
Process finished with exit code 0



                   



An interesting talk by the creator of CouchDB

I watched this presentation which is really inspiring, by Damien Katz who developed a database using Erlang(a functional language).He talks about the circumstances and hardships he had while developing a new db even there are others in industry which has become integral part of infrastructure.Cool people make cool stuffs !!

http://www.infoq.com/presentations/katz-couchdb-and-me

What so interesting about CouchDB ?
In CouchDB, the data is a collection of JSON documents.It is more of object database than a relational db.I shows how powerful is javascript in server side -->Views are created by javascript like a map reduce.It is a very good choice to use for scalabale RESTful applications.Currently this project is in alpha stage.

More about CouchDB http://couchdb.apache.org/docs/intro.html

Applications using CouchDB http://wiki.apache.org/couchdb/CouchDB_in_the_wild

Death of ECMA 4 and future of Native JSON parsing



JSON is a subset of JavaScript.It is used to represent data as tokens of name - value pairs.This technology provides an efficient dataportability for mashable appliations.The JSON syntax is like JavaScript's object literal syntax except that the objects cannot be assigned to a variable. JSON just represents the data itself.Data is the string.This literal has to be converted to object.We can use eval() in Javascript to evaluate script.But using eval() is harmful.So a JSON parser will recognize only JSON text, rejecting all scripts.




If u need a javascript parser use ..

JSON.parse(strJSON) - converts a JSON string into a JavaScript object.
JSON.stringify(objJSON) - converts a JavaScript object into a JSON string.
Other parsers are

Jackson JSON Processor based on , STreaming Api for Xml processing .

JSON-lib is a java library for transforming beans, maps, collections, java arrays and XML to JSON and back again to beans and DynaBeans.

If you are using mozilla firefox and your browser supported above gecko 1.9, there is a regexp:test() function.

According to specification proposed by D.Crockford :

A JSON text can be safely passed into JavaScript's eval() function (which compiles and executes a string) if all the characters not enclosed in strings are in the set of characters that form JSON tokens. This can be quickly determined in JavaScript with two regular expressions and calls to the test and replace methods.

var my_JSON_object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
eval('(' + text + ')');


This particular technology is alternative to XML to port data.The webservices implementations return XML data.The applications use parsers to generate native objects from these XML.XML is the data used in AJAX, which became the buzz in new age web.The data format is vital to the efficiency of any application.JSON provides an efficient data transfer.

Read JSON: The Fat-Free Alternative to XML

If you want to add JSON to application , read here how Yahoo webservices implemented this.

According to John Resig, browsers should support native json support,

He summarises that

The current, recommended, implementation of JSON parsing and serialization is harmful and slow. Additionally, upcoming standards imply that a native JSON (de-)serializer already exists. Therefore, browsers should be seriously looking at defining a standard for native JSON support, and upon completion implement it quickly and broadly.
Read here for the ECMA4 proposal

but now ?

ECMAScript 4.0 Is Dead


JavaScript standards wrangle swings Microsoft's way

The industry is again having war on browsers and internet standards.

So different methodologies to make JSON will be exist for some years on.As the system become complex, vulnerabilites will arise and we have to findout resolutions for those issues every time.