org.boon.json.JsonSerializerFactory Java Examples

The following examples show how to use org.boon.json.JsonSerializerFactory. 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: Bug247.java    From boon with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {

    JsonSerializer serializer
            = new JsonSerializerFactory()
            .addTypeSerializer(AbstractMap.class, new MapSerializer()).create();
    Map<String, String> map = map("Aaaaa", "aaaa", "Bbbbb", "bbbb", "Ccccc", "cccc");
    Predicate<String> startsWithB = new Predicate<String>() {
        @Override
        public boolean apply(String s) {
            return s.charAt(0) == 'B';
        }
    };
    Map<String, String> filtered = Maps.filterKeys(map, startsWithB);
    String result = serializer.serialize(filtered).toString();

    ok = result.equals("{\"Bbbbb\":\"bbbb\"}") || die(result);
}
 
Example #2
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 #3
Source File: DataStoreSimpleHttpClient.java    From boon with Apache License 2.0 6 votes vote down vote up
public void init(DataOutputQueue queue, String clientId, String url, boolean verbose) {
    this.queue = queue;
    this.url = url;


    if (clientId == null) {
        this.clientId = UUID.randomUUID().toString();
    } else {
        this.clientId = clientId;
    }


    if (queue == null) {
        this.queue = new DataOutputQueueTransferQueue(1000);
    }
    JsonSerializerFactory factory = new JsonSerializerFactory();
    serializer = factory.create();
    this.verbose = verbose;
}
 
Example #4
Source File: Bug199.java    From boon with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {

    JsonSerializerFactory factory = new JsonSerializerFactory();
    factory.usePropertyOnly();
    ObjectMapper mapper = JsonFactory.create(null, factory);

    User user = new User();
    EnumMap<Gender,User> map = new EnumMap<Gender, User>(Gender.class);
    map.put(Gender.FEMALE, user);

    puts(map);
    puts(mapper.writeValueAsString( map));
}
 
Example #5
Source File: StatsResults.java    From boon with Apache License 2.0 5 votes vote down vote up
private String toJson() {
    if (jsonSerializerThreadLocal.get() == null) {
        jsonSerializerThreadLocal.set(new JsonSerializerFactory().create());
    }

    return jsonSerializerThreadLocal.get().serialize(this).toString();
}
 
Example #6
Source File: TestBoon.java    From jdbc-cb with Apache License 2.0 4 votes vote down vote up
@Test
public void testFlatten1()
{
    ClassLoader classLoader = getClass().getClassLoader();
    URL url = classLoader.getResource("example1.json");
    String path = url.getFile();
    File file = new File(path);

    ValueMap jsonObject = (ValueMap)new JsonSlurper().parse(file);

    ValueList results  = (ValueList)jsonObject.get("results");

    JsonSerializer jsonSerializer = new JsonSerializerFactory().outputType().create();


    System.out.println(jsonSerializer.serialize(jsonObject));

}
 
Example #7
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 #8
Source File: Bugs231.java    From boon with Apache License 2.0 4 votes vote down vote up
private static JsonSerializer createSerializer() {
    JsonSerializerFactory factory = new JsonSerializerFactory()
            .addTypeSerializer(HashCode.class, new HashCodeSerializer()
            );
    return factory.create();
}
 
Example #9
Source File: ABug7.java    From boon with Apache License 2.0 4 votes vote down vote up
@Test
public void testListComplex() {



    JsonSerializer jsonSerializer = new JsonSerializerFactory().setUseAnnotations(true)
            .addFilter(new FieldFilter() {
                @Override
                public boolean include(Object parent, FieldAccess fieldAccess) {
                    return !fieldAccess.name().equals("metaClass");
                }
            }).create();

    final String json = jsonSerializer.serialize(Lists.list(1, 2, null, 3)).toString();

    assertNotNull(json);

    assertEquals("[1,2,null,3]", json);

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



    JsonSerializer jsonSerializer = new JsonSerializerFactory().setUseAnnotations(true)
            .addFilter(new FieldFilter() {
                @Override
                public boolean include(Object parent, FieldAccess fieldAccess) {
                    return !fieldAccess.name().equals("metaClass");
                }
            }).create();

    final String json = jsonSerializer.serialize(Arry.array(1, 2, null, 3)).toString();

    assertNotNull(json);

    assertEquals("[1,2,null,3]", json);

}
 
Example #11
Source File: Bug232.java    From boon with Apache License 2.0 4 votes vote down vote up
@Test
public void test() {
    JsonSerializerFactory factory = new JsonSerializerFactory();
    final JsonSerializer jsonSerializer = factory.includeNulls().create();

    final String json = jsonSerializer.serialize(

            Maps.map("job", "programmer",
                    "age", null,
                    "showSize", 12)
    ).toString();


    ok |= json.contains("\"age\":null") || die();


}
 
Example #12
Source File: ABug7.java    From boon with Apache License 2.0 3 votes vote down vote up
@Test
public void testListSimple() {



    JsonSerializer jsonSerializer = new JsonSerializerFactory().create();

    final String json = jsonSerializer.serialize(Lists.list(1, 2, null, 3)).toString();

    assertNotNull(json);

    assertEquals("[1,2,null,3]", json);

}
 
Example #13
Source File: ABug7.java    From boon with Apache License 2.0 3 votes vote down vote up
@Test
public void testArraySimple() {



    JsonSerializer jsonSerializer = new JsonSerializerFactory().create();

    final String json = jsonSerializer.serialize(Arry.array(1, 2, null, 3)).toString();

    assertNotNull(json);

    assertEquals("[1,2,null,3]", json);

}