Java Code Examples for org.boon.json.ObjectMapper#toJson()

The following examples show how to use org.boon.json.ObjectMapper#toJson() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bug199_2.java    From boon with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    Builder builder = new Builder();
    MetadataImpl metadataImpl = builder.getMetadata();

    JsonSerializerFactory jsonSerializerFactory=new JsonSerializerFactory().usePropertyOnly();
    ObjectMapper mapper = JsonFactory.create(null, jsonSerializerFactory);
    String json = mapper.writeValueAsString(metadataImpl);
    System.out.println("=============" + json);


    File file = new File("metadata.json");
    FileWriter writer = new FileWriter(file);
    mapper.toJson(metadataImpl, writer);
    writer.close();
    Path path = Paths.get(file.toString());
    InputStream inputStream = Files.newInputStream(path);


    MetadataImpl object = JsonFactory.create().readValue(inputStream,
            MetadataImpl.class);
    inputStream.close();

    System.out.println("after deserialization"
            + mapper.writeValueAsString(object));

}
 
Example 2
Source File: Bug312.java    From boon with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithMapUsingFactory() {
    JsonParserFactory jsonParserFactory = new JsonParserFactory()
            .useFieldsFirst()
            .lax() //allow loose parsing of JSON like JSON Smart
            .setCharset( StandardCharsets.UTF_8 ) //Set the standard charset, defaults to UTF_8
            .setLazyChop( true ) //similar to chop but only does it after map.get
            ;

    JsonSerializerFactory jsonSerializerFactory = new JsonSerializerFactory()
            .useFieldsFirst() //one of these
                    //.addPropertySerializer(  )  customize property output
                    //.addTypeSerializer(  )      customize type output
            .useJsonFormatForDates() //use json dates
                    //.addFilter(  )   add a property filter to exclude properties
            .includeEmpty().includeNulls().includeDefaultValues() //override defaults
            .handleComplexBackReference() //uses identity map to track complex back reference and avoid them
            .setHandleSimpleBackReference( true ) //looks for simple back reference for parent
            .setCacheInstances( true ) //turns on caching for immutable objects
            ;

    final ObjectMapper objectMapper = JsonFactory.create(jsonParserFactory, jsonSerializerFactory);


    MyClass myClass = new MyClass();
    final String json = objectMapper.toJson(myClass);

    puts(json);

    final MyClass myClass1 = objectMapper.readValue(json, MyClass.class);


    assertEquals("foo", myClass1.string);

    assertEquals(1, myClass1.integer);


    assertNull(myClass1.map);


}
 
Example 3
Source File: Bug297.java    From boon with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

            // create object
            MyObject myobject = new MyObject();
            // add a title and attributes
            myobject.setTitle("title with \" double qoutes");
            Map<String,Object> attributes = new HashMap<>();
            attributes.put("author", "author with \" double quotes");
            myobject.setAttrs(attributes);

            // create ObjectMapper
            ObjectMapper mapper = JsonFactory.create();

            // serialize myobject to json
            String myobjectJson = mapper.toJson(myobject);
            System.out.println(myobjectJson);
            // prints: {"title":"title with \" double qoutes","attrs":{"author":"author with \" double quotes"}}
            // parse myobjectJson to an object
            MyObject myobjectParsed = mapper.fromJson(myobjectJson, MyObject.class);

            final boolean equals = myobjectParsed.equals(myobject);

            System.out.println("equals " + equals + " " + myobjectParsed.title);

            System.out.println(myobjectParsed.title);

            System.out.println(myobjectParsed.getAttrs().get("author").getClass().getName());


            puts("\n", myobjectParsed, "\n", myobject);

            // serialize again my myobjectParsed to json
            myobjectJson = mapper.toJson(myobjectParsed);
            System.out.println(myobjectJson);
            // double quoted for attrs are not escaped
            // prints: {"title":"title with \" double qoutes","attrs":{"author":"author with " double quotes"}}


            MyObject myobjectParsed2 = mapper.fromJson(myobjectJson,MyObject.class);
            // Exception in thread "main" org.boon.json.JsonException: expecting current character to be ':' but got '}' with an int value of 125

        }
 
Example 4
Source File: Bug287.java    From boon with Apache License 2.0 4 votes vote down vote up
@Test
public void serializingClassFieldCausesSegFault() {

    SomeClass someClassInstance = new SomeClass(Bug287.class);

    ObjectMapper mapper = JsonFactory.create();

    final String json = mapper.toJson(someClassInstance);

    puts(json);

    SomeClass someClassInstance2 = mapper.readValue("{\"clazz\":\"org.boon.bugs.Bug287\"} ", SomeClass.class);

    ok = someClassInstance2.clazz.getName().equals("org.boon.bugs.Bug287");

}
 
Example 5
Source File: BugReport165AndIssue169.java    From boon with Apache License 2.0 3 votes vote down vote up
@Test
public void test2() {

    Employee employee = new Employee();

    ObjectMapper mapper = JsonFactory.create();

    String json = mapper.toJson(employee);

    puts(json);

    employee.name = "Rick Hightower";

    json = mapper.toJson(employee);

    puts(json);

    employee.birthDate = System.currentTimeMillis() - 60 * 1000 * 24 * 7 * 52 * 29;


    json = mapper.toJson(employee);

    puts(json);

    Employee newEmployee = mapper.fromJson(json, Employee.class);

    puts("New Employee", newEmployee.birthDate, newEmployee.name);


    ok = newEmployee.name.equals("Rick Hightower") && newEmployee.birthDate > 0 || die();

}
 
Example 6
Source File: BugReport165AndIssue169.java    From boon with Apache License 2.0 3 votes vote down vote up
@Test
public void test3() {

    Employee employee = new Employee();

    ObjectMapper mapper = new ObjectMapperImpl();
    String json = mapper.toJson(employee);

    puts(json);

    employee.name = "Rick Hightower";

    json = mapper.toJson(employee);

    puts(json);

    employee.birthDate = System.currentTimeMillis() - 60 * 1000 * 24 * 7 * 52 * 29;


    json = mapper.toJson(employee);

    puts(json);

    Employee newEmployee = mapper.fromJson(json, Employee.class);

    puts("New Employee", newEmployee.birthDate, newEmployee.name);


    ok = newEmployee.name.equals("Rick Hightower") && newEmployee.birthDate > 0;



}