tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > FLEX JSON > Serialize JSON String to OutputHandler

Serialize JSON String to OutputHandler 

Flexjson is a lightweight java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from https://sourceforge.net/projects/flexjson/. This requires the library flexjson-2.1.jar to be in classpath. The following example shows how to serialize a Java Object to the given OutputHandler.

File Name  :  
com/bethecoder/tutorials/flexjson/tests/SerializeTest2.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.flexjson.tests;

import java.io.StringWriter;
import java.util.Date;

import com.bethecoder.tutorials.flexjson.common.Student;

import flexjson.JSONSerializer;
import flexjson.OutputHandler;
import flexjson.WriterOutputHandler;

public class SerializeTest2 {

  /**
   @param args
   */
  public static void main(String[] args) {

    JSONSerializer serializer = new JSONSerializer().prettyPrint(true)
    Student stud = new Student("Sriram""Kasireddi"2"Singing"new Date());
    
    OutputHandler out = new WriterOutputHandler(new StringWriter());
    serializer.serialize(stud, out);
    
    System.out.println(out.toString());
  }

}
   

It gives the following output,
{
    "age": 2,
    "class": "com.bethecoder.tutorials.flexjson.common.Student",
    "dob": 1341598167640,
    "firstName": "Sriram",
    "hobby": "Singing",
    "lastName": "Kasireddi"
}



 
  


  
bl  br