当前位置: 首页>>代码示例>>Java>>正文


Java Jsonb.toJson方法代码示例

本文整理汇总了Java中javax.json.bind.Jsonb.toJson方法的典型用法代码示例。如果您正苦于以下问题:Java Jsonb.toJson方法的具体用法?Java Jsonb.toJson怎么用?Java Jsonb.toJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.json.bind.Jsonb的用法示例。


在下文中一共展示了Jsonb.toJson方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getIt

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
/**
 * Method handling HTTP GET requests. The returned object will be sent
 * to the client as "text/plain" media type.
 *
 * @return String that will be returned as a text/plain response.
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getIt() {
    JsonbConfig config = new JsonbConfig();

    config.withAdapters(new EntityAdapter());
    Jsonb jsonb = JsonbBuilder.create(config);

    CEntity entity = new EntityImpl("urn:c3im:Vehicle:4567", "Vehicle");
    CProperty propertySt = new CPropertyImpl("speed", 40);

    entity.addProperty(propertySt);

    return jsonb.toJson(entity);
}
 
开发者ID:Fiware,项目名称:NGSI-LD_Wrapper,代码行数:22,代码来源:MyResourceJson2.java

示例2: givenSerialize_shouldSerialiseMagazine

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void givenSerialize_shouldSerialiseMagazine() {

    Magazine magazine = new Magazine();
    magazine.setId("1234-QWERT");
    magazine.setTitle("Fun with Java");
    magazine.setAuthor(new Author("Alex","Theedom"));

    String expectedJson = "{\"name\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}}";

    JsonbConfig config = new JsonbConfig().withSerializers(new MagazineSerializer());
    Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build();
    String actualJson = jsonb.toJson(magazine);
    assertThat(actualJson).isEqualTo(expectedJson);

}
 
开发者ID:readlearncode,项目名称:Java-EE-8-Sampler,代码行数:17,代码来源:MagazineSerializerTest.java

示例3: personToJsonString

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void personToJsonString() {
    Person duke = new Person("Duke", LocalDate.of(1995, 5, 23));
    duke.setPhoneNumbers(
            Arrays.asList(
                    new PhoneNumber(HOME, "100000"),
                    new PhoneNumber(OFFICE, "200000")
            )
    );

    Jsonb jsonMapper = JsonbBuilder.create();
    String json = jsonMapper.toJson(duke);

    LOG.log(Level.INFO, "converted json result: {0}", json);

    String name = JsonPath.parse(json).read("$.name");
    assertEquals("Duke", name);

    Configuration config = Configuration.defaultConfiguration()
            .jsonProvider(new GsonJsonProvider())
            .mappingProvider(new GsonMappingProvider());
    TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {
    };

    List<String> numbers = JsonPath.using(config).parse(json).read("$.phoneNumbers[*].number", typeRef);

    assertEquals(Arrays.asList("100000", "200000"), numbers);
}
 
开发者ID:hantsy,项目名称:ee8-sandbox,代码行数:29,代码来源:JsonbTest.java

示例4: testConflictingProperties

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
/**
 * Same problem as above but now field is public, so clash takes place.
 */
@Test
public void testConflictingProperties() {
    ConflictingProperties conflictingProperties = new ConflictingProperties();
    conflictingProperties.setDOI("DOI value");
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig()
    );

    try {
        jsonb.toJson(conflictingProperties);
        fail();
    } catch (JsonbException e) {
        if (!e.getMessage().equals("Property DOI clashes with property doi by read or write name in class org.eclipse.yasson.customization.JsonbPropertyTest$ConflictingProperties.")) {
            throw e;
        }
    }
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:20,代码来源:JsonbPropertyTest.java

示例5: testConflictingWithUpperCamelStrategy

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
/**
 * Tests clash with property altered by naming strategy.
 */
@Test
public void testConflictingWithUpperCamelStrategy() {
    ConflictingWithUpperCamelStrategy pojo = new ConflictingWithUpperCamelStrategy();
    pojo.setDOI("DOI value");

    Jsonb jsonb = JsonbBuilder.create();
    String json = jsonb.toJson(pojo);
    Assert.assertEquals("{\"Doi\":\"DOI value\",\"doi\":\"DOI value\"}", json);

    jsonb = JsonbBuilder.create(new JsonbConfig()
            .withPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE));

    try {
        jsonb.toJson(pojo);
        fail();
    } catch (JsonbException e) {
        if (!e.getMessage().equals("Property DOI clashes with property doi by read or write name in class org.eclipse.yasson.customization.JsonbPropertyTest$ConflictingWithUpperCamelStrategy.")) {
            throw e;
        }
    }

}
 
开发者ID:eclipse,项目名称:yasson,代码行数:26,代码来源:JsonbPropertyTest.java

示例6: testContainerSerializer

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void testContainerSerializer() {
    Jsonb jsonb = JsonbBuilder.create();

    SimpleAnnotatedSerializedArrayContainer container = new SimpleAnnotatedSerializedArrayContainer();
    SimpleContainer instance1 = new SimpleContainer();
    instance1.setInstance("Test String 1");
    SimpleContainer instance2 = new SimpleContainer();
    instance2.setInstance("Test String 2");
    container.setArrayInstance(new SimpleContainer[] {instance1, instance2});

    container.setListInstance(new ArrayList<>());
    container.getListInstance().add(new SimpleContainer("Test List 1"));
    container.getListInstance().add(new SimpleContainer("Test List 2"));

    String jsonString = jsonb.toJson(container);
    Assert.assertEquals("{\"arrayInstance\":[{\"instance\":\"Test String 1\"},{\"instance\":\"Test String 2\"}],\"listInstance\":[{\"instance\":\"Test List 1\"},{\"instance\":\"Test List 2\"}]}", jsonString);

    SimpleAnnotatedSerializedArrayContainer unmarshalledObject = jsonb.fromJson("{\"arrayInstance\":[{\"instance\":\"Test String 1\"},{\"instance\":\"Test String 2\"}],\"listInstance\":[{\"instance\":\"Test List 1\"},{\"instance\":\"Test List 2\"}]}", SimpleAnnotatedSerializedArrayContainer.class);

    Assert.assertEquals("Test String 1", unmarshalledObject.getArrayInstance()[0].getInstance());
    Assert.assertEquals("Test String 2", unmarshalledObject.getArrayInstance()[1].getInstance());

    Assert.assertEquals("Test List 1", unmarshalledObject.getListInstance().get(0).getInstance());
    Assert.assertEquals("Test List 2", unmarshalledObject.getListInstance().get(1).getInstance());
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:27,代码来源:SerializersTest.java

示例7: testMarshallRawList

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testMarshallRawList() throws ParseException {
    List rawList = new ArrayList();
    final SimpleDateFormat ddMMyyyy = new SimpleDateFormat("ddMMyyyy");
    ddMMyyyy.setTimeZone(TimeZone.getTimeZone("Z"));
    rawList.add(ddMMyyyy.parse("24031981"));
    Box box = new Box();
    box.boxStr = "box string";
    box.crate = new Crate();
    box.crate.crateStr = "crate str";
    rawList.add(box);

    final Jsonb jsonb = JsonbBuilder.create();
    String result = jsonb.toJson(rawList);
    assertEquals("[\"1981-03-24T00:00:00Z[UTC]\",{\"boxStr\":\"box string\",\"crate\":{\"crate_str\":\"crate str\"}}]", result);
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:18,代码来源:GenericsTest.java

示例8: testDateInMap

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void testDateInMap() {

    JsonbConfig config = new JsonbConfig()
            .withDateFormat("yyyy", Locale.ENGLISH);

    Jsonb jsonb = JsonbBuilder.create(config);
    LocalDate localDate = LocalDate.of(2017, 9, 14);

    DateInMapPojo pojo = new DateInMapPojo();
    pojo.setLocalDate(localDate);
    pojo.setDateMap(new HashMap<>());
    pojo.getDateMap().put("first", localDate);

    String json = jsonb.toJson(pojo);

    Assert.assertEquals("{\"dateMap\":{\"first\":\"2017\"},\"localDate\":\"2017\"}", json);

    config = new JsonbConfig()
            .withDateFormat("dd.MM.yyyy", Locale.ENGLISH);
    jsonb = JsonbBuilder.create(config);
    DateInMapPojo result = jsonb.fromJson("{\"dateMap\":{\"first\":\"01.01.2017\"},\"localDate\":\"01.01.2017\"}", DateInMapPojo.class);
    Assert.assertEquals(LocalDate.of(2017,1,1), result.localDate);
    Assert.assertEquals(LocalDate.of(2017,1,1), result.dateMap.get("first"));
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:26,代码来源:DatesTest.java

示例9: testWithSecurityManager

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void testWithSecurityManager() {
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            return Modifier.isPublic(field.getModifiers()) || field.getName().equals("privateProperty");
        }

        @Override
        public boolean isVisible(Method method) {
            return Modifier.isPublic(method.getModifiers());
        }
    }));

    Pojo pojo = new Pojo();
    pojo.setStrProperty("string propery");
    Crate crate = new Crate();
    crate.crateBigDec = BigDecimal.TEN;
    crate.crateStr = "crate string";
    pojo.setCrate(crate);

    String result = jsonb.toJson(pojo);
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:24,代码来源:SecurityManagerTest.java

示例10: testNonCdiEnvironment

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void testNonCdiEnvironment() {
    JsonbConfig config = new JsonbConfig();
    //allow only field with components that doesn't has cdi dependencies.
    config.withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
        @Override
        public boolean isVisible(Field field) {
            return "adaptedValue3".equals(field.getName());
        }

        @Override
        public boolean isVisible(Method method) {
            return false;
        }
    });
    Jsonb jsonb = JsonbBuilder.create(config);
    final String result = jsonb.toJson(new AdaptedPojo());
    assertEquals("{\"adaptedValue3\":1010}", result);
}
 
开发者ID:eclipse,项目名称:yasson,代码行数:20,代码来源:CdiInjectionTest.java

示例11: adapt

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void adapt() {
    final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withAdapters(new BarAdapter()));
    final Foo foo = new Foo();
    foo.bar = new Bar();
    foo.bar.value = 1;
    foo.dummy = new Dummy();
    foo.dummy.value = 2L;

    final String toString = jsonb.toJson(foo);
    assertEquals("{\"bar\":\"1\",\"dummy\":\"2\"}", toString);

    final Foo read = jsonb.fromJson(toString, Foo.class);
    assertEquals(foo.bar.value, read.bar.value);
    assertEquals(foo.dummy.value, read.dummy.value);
}
 
开发者ID:apache,项目名称:johnzon,代码行数:17,代码来源:AdapterTest.java

示例12: customizedMapping

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
public String customizedMapping() {

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
 
开发者ID:readlearncode,项目名称:Java-EE-8-Sampler,代码行数:14,代码来源:JsonBindingExample.java

示例13: allCustomizedMapping

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
public String allCustomizedMapping() {

        PropertyVisibilityStrategy vis = new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                return false;
            }

            @Override
            public boolean isVisible(Method method) {
                return false;
            }
        };

        JsonbConfig jsonbConfig = new JsonbConfig()
                .withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
                .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL)
                .withPropertyVisibilityStrategy(vis)
                .withStrictIJSON(true)
                .withFormatting(true)
                .withNullValues(true)
                .withBinaryDataStrategy(BinaryDataStrategy.BASE_64_URL)
                .withDateFormat("MM/dd/yyyy", Locale.ENGLISH);

        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);

        return jsonb.toJson(book1);
    }
 
开发者ID:readlearncode,项目名称:Java-EE-8-Sampler,代码行数:29,代码来源:JsonBindingExample.java

示例14: deserializeBook

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
public Book deserializeBook() {
    Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom");
    Jsonb jsonb = JsonbBuilder.create();
    String json = jsonb.toJson(book);
    book = jsonb.fromJson(json, Book.class);
    return book;
}
 
开发者ID:readlearncode,项目名称:Java-EE-8-Sampler,代码行数:8,代码来源:MinimalExample.java

示例15: givenSerialize_shouldSerialiseBook

import javax.json.bind.Jsonb; //导入方法依赖的package包/类
@Test
public void givenSerialize_shouldSerialiseBook() {
    Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom");
    String expectedJson = "{\"com.readlearncode.devWorks.overview.domain.Book\":{\"author\":\"Alex Theedom\",\"id\":\"SHDUJ-4532\",\"title\":\"Fun with Java\"}}";
    JsonbConfig config = new JsonbConfig().withSerializers(new BookSerializer());
    Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build();
    String actualJson = jsonb.toJson(book);
    assertThat(actualJson).isEqualTo(expectedJson);
}
 
开发者ID:readlearncode,项目名称:Java-EE-8-Sampler,代码行数:10,代码来源:BookSerializerTest.java


注:本文中的javax.json.bind.Jsonb.toJson方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。