本文整理汇总了Java中javax.json.bind.Jsonb类的典型用法代码示例。如果您正苦于以下问题:Java Jsonb类的具体用法?Java Jsonb怎么用?Java Jsonb使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Jsonb类属于javax.json.bind包,在下文中一共展示了Jsonb类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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: serialize
import javax.json.bind.Jsonb; //导入依赖的package包/类
@Override
public byte[] serialize(final String topic, final CoffeeEvent event) {
try {
if (event == null)
return null;
final JsonbConfig config = new JsonbConfig()
.withAdapters(new UUIDAdapter())
.withSerializers(new EventJsonbSerializer());
final Jsonb jsonb = JsonbBuilder.create(config);
return jsonb.toJson(event, CoffeeEvent.class).getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
logger.severe("Could not serialize event: " + e.getMessage());
throw new SerializationException("Could not serialize event", e);
}
}
开发者ID:sdaschner,项目名称:scalable-coffee-shop,代码行数:19,代码来源:EventSerializer.java
示例4: get
import javax.json.bind.Jsonb; //导入依赖的package包/类
/**
* accept = application/json
* @param <T>
* @param url
* @param entity
* @return ClientResponse
* @throws java.io.IOException
*/
public static <T> T get(String url,Class<?> entity) throws IOException{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return (T) jsonb.fromJson(response.body().string(), entity);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:25,代码来源:DiavgeiaHttpUtils.java
示例5: getOrganization
import javax.json.bind.Jsonb; //导入依赖的package包/类
/**
* accept = application/json
* @param url
* @return OrganizationDao
* @throws java.io.IOException
*/
public static OrganizationDao getOrganization(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), OrganizationDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:23,代码来源:DiavgeiaHttpUtils.java
示例6: getDecisionTypeDetailsDao
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static DecisionTypeDetailsDao getDecisionTypeDetailsDao(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), DecisionTypeDetailsDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例7: getDictionaryItemsDao
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static DictionaryItemsDao getDictionaryItemsDao(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), DictionaryItemsDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例8: getSignersDao
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static SignersDao getSignersDao(String url) throws IOException{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), SignersDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例9: getUnitsDao
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static UnitsDao getUnitsDao(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), UnitsDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例10: getDictionaryItems
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static DictionaryItemsDao getDictionaryItems(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), DictionaryItemsDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例11: getTypes
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static DecisionTypesDao getTypes(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), DecisionTypesDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例12: getDecision
import javax.json.bind.Jsonb; //导入依赖的package包/类
public static DecisionDao getDecision(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.header("Accept", "application/json")
.get()
.build();
Response response = client.newCall(request).execute();
client.dispatcher().executorService().shutdown();
if(response.code() == 200){
Jsonb jsonb = JsonbBuilder.create();
return jsonb.fromJson(response.body().string(), DecisionDao.class);
}
return null;
}
开发者ID:avraampiperidis,项目名称:DiavgeiaUtils,代码行数:17,代码来源:DiavgeiaHttpUtils.java
示例13: 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
示例14: 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
示例15: 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
示例16: testFieldVisibilityStrategy
import javax.json.bind.Jsonb; //导入依赖的package包/类
/**
* Tests applying for both public and nonpublic fields.
*/
@Test
public void testFieldVisibilityStrategy() {
JsonbConfig customizedConfig = new JsonbConfig();
customizedConfig.setProperty(JsonbConfig.PROPERTY_VISIBILITY_STRATEGY, new PropertyVisibilityStrategy() {
@Override
public boolean isVisible(Field field) {
final String fieldName = field.getName();
return fieldName.equals("afield") || fieldName.equals("dfield");
}
@Override
public boolean isVisible(Method method) {
throw new IllegalStateException("Not supported");
}
});
FieldPojo fieldPojo = new FieldPojo("avalue", "bvalue", "cvalue", "dvalue");
Jsonb jsonb = JsonbBuilder.create(customizedConfig);
assertEquals("{\"afield\":\"avalue\",\"dfield\":\"dvalue\"}", jsonb.toJson(fieldPojo));
}
开发者ID:eclipse,项目名称:yasson,代码行数:25,代码来源:JsonbPropertyVisibilityStrategyTest.java
示例17: testMethodVisibilityStrategy
import javax.json.bind.Jsonb; //导入依赖的package包/类
/**
* Tests applying for both public and nonpublic getters.
*/
@Test
public void testMethodVisibilityStrategy() {
JsonbConfig customizedConfig = new JsonbConfig();
customizedConfig.setProperty(JsonbConfig.PROPERTY_VISIBILITY_STRATEGY, new PropertyVisibilityStrategy() {
@Override
public boolean isVisible(Field field) {
throw new IllegalStateException("Not supported");
}
@Override
public boolean isVisible(Method method) {
final String methodName = method.getName();
return methodName.equals("getAgetter") || methodName.equals("getDgetter");
}
});
GetterPojo getterPojo = new GetterPojo();
Jsonb jsonb = JsonbBuilder.create(customizedConfig);
assertEquals("{\"agetter\":\"avalue\",\"dgetter\":\"dvalue\"}", jsonb.toJson(getterPojo));
}
开发者ID:eclipse,项目名称:yasson,代码行数:25,代码来源:JsonbPropertyVisibilityStrategyTest.java
示例18: testMarshallOptionalIntArray
import javax.json.bind.Jsonb; //导入依赖的package包/类
@Test
public void testMarshallOptionalIntArray() {
final Jsonb jsonb = (new JsonBindingBuilder()).build();
final OptionalInt[] array = {OptionalInt.of(1), OptionalInt.of(2), OptionalInt.empty()};
assertEquals("[1,2,null]", jsonb.toJson(array));
OptionalInt[] result = jsonb.fromJson("[1,2,null]", new TestTypeToken<OptionalInt[]>() {}.getType());
assertTrue(result[0].isPresent());
assertEquals(1, result[0].getAsInt());
assertTrue(result[1].isPresent());
assertEquals(2, result[1].getAsInt());
assertEquals(OptionalInt.empty(), result[2]);
}
开发者ID:eclipse,项目名称:yasson,代码行数:18,代码来源:OptionalTest.java
示例19: 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
示例20: testDifferentConfigsLocalDateTime
import javax.json.bind.Jsonb; //导入依赖的package包/类
@Test
public void testDifferentConfigsLocalDateTime() {
final LocalDateTime dateTime = LocalDateTime.of(2015, 2, 16, 13, 21);
final long millis = dateTime.atZone(ZoneId.of("Z")).toInstant().toEpochMilli();
final ScalarValueWrapper<LocalDateTime> pojo = new ScalarValueWrapper<>();
pojo.setValue(dateTime);
final String expected = "{\"value\":\"2015-02-16T13:21:00\"}";
assertEquals(expected, jsonb.toJson(pojo));
final Jsonb jsonbCustom = JsonbBuilder.create(new JsonbConfig().withDateFormat(JsonbDateFormat.TIME_IN_MILLIS, Locale.FRENCH));
assertEquals("{\"value\":\"" + millis + "\"}", jsonbCustom.toJson(pojo));
ScalarValueWrapper<LocalDateTime> result = this.jsonb.fromJson(expected, new TestTypeToken<ScalarValueWrapper<LocalDateTime>>(){}.getType());
assertEquals(dateTime, result.getValue());
result = jsonbCustom.fromJson("{\"value\":\"" + millis + "\"}", new TestTypeToken<ScalarValueWrapper<LocalDateTime>>(){}.getType());
assertEquals(dateTime, result.getValue());
}
开发者ID:eclipse,项目名称:yasson,代码行数:20,代码来源:DatesTest.java
注:本文中的javax.json.bind.Jsonb类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论