本文整理汇总了Java中javax.json.bind.Jsonb类的典型用法代码示例。如果您正苦于以下问题:Java Jsonb类的具体用法?Java Jsonb怎么用?Java Jsonb使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Jsonb类属于javax.json.bind包,在下文中一共展示了Jsonb类的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);
}
示例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);
}
示例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);
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
}
}
示例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;
}
}
}