本文整理匯總了Java中com.google.gson.JsonSerializer類的典型用法代碼示例。如果您正苦於以下問題:Java JsonSerializer類的具體用法?Java JsonSerializer怎麽用?Java JsonSerializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JsonSerializer類屬於com.google.gson包,在下文中一共展示了JsonSerializer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: configureGsonBuilder
import com.google.gson.JsonSerializer; //導入依賴的package包/類
private GsonBuilder configureGsonBuilder() {
GsonBuilder gsonBuilder = new GsonBuilder();
JsonSerializer<NavigationArriveEvent> arriveSerializer = new ArriveEventSerializer();
gsonBuilder.registerTypeAdapter(NavigationArriveEvent.class, arriveSerializer);
JsonSerializer<NavigationDepartEvent> departSerializer = new DepartEventSerializer();
gsonBuilder.registerTypeAdapter(NavigationDepartEvent.class, departSerializer);
JsonSerializer<NavigationCancelEvent> cancelSerializer = new CancelEventSerializer();
gsonBuilder.registerTypeAdapter(NavigationCancelEvent.class, cancelSerializer);
JsonSerializer<NavigationFeedbackEvent> feedbackSerializer = new FeedbackEventSerializer();
gsonBuilder.registerTypeAdapter(NavigationFeedbackEvent.class, feedbackSerializer);
JsonSerializer<NavigationRerouteEvent> rerouteSerializer = new RerouteEventSerializer();
gsonBuilder.registerTypeAdapter(NavigationRerouteEvent.class, rerouteSerializer);
JsonSerializer<NavigationFasterRouteEvent> fasterRouteSerializer = new FasterRouteEventSerializer();
gsonBuilder.registerTypeAdapter(NavigationFasterRouteEvent.class, fasterRouteSerializer);
return gsonBuilder;
}
示例2: print
import com.google.gson.JsonSerializer; //導入依賴的package包/類
@Override
protected void print(ExportHelper helper, EventLookupRpcRequest request, List<EventInterface> events, int eventCookieFlags, EventMeetingSortBy sort, boolean asc) throws IOException {
helper.setup("application/json", reference(), false);
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
}
})
.setFieldNamingStrategy(new FieldNamingStrategy() {
Pattern iPattern = Pattern.compile("i([A-Z])(.*)");
@Override
public String translateName(Field f) {
Matcher matcher = iPattern.matcher(f.getName());
if (matcher.matches())
return matcher.group(1).toLowerCase() + matcher.group(2);
else
return f.getName();
}
})
.setPrettyPrinting().create();
helper.getWriter().write(gson.toJson(events));
}
示例3: HanaDBTemplate
import com.google.gson.JsonSerializer; //導入依賴的package包/類
public HanaDBTemplate() {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Point.class, (JsonSerializer<Point>) (Point src, Type type, JsonSerializationContext context) -> {
JsonObject obj = new JsonObject();
// serialize it all to strings for reasons..
obj.addProperty("ZEITREIHE", src.getTimeseries());
obj.addProperty("TIME", String.valueOf(src.getTimestamp()));
obj.addProperty("VALUE", src.getValue().toString());
return obj;
});
gson = builder.create();
OkHttpClient.Builder okHBuilder = new OkHttpClient.Builder();
okHBuilder.readTimeout(30, TimeUnit.SECONDS);
okHBuilder.writeTimeout(30, TimeUnit.SECONDS);
okHttpClient = okHBuilder.build();
}
示例4: newSettingsSerializer
import com.google.gson.JsonSerializer; //導入依賴的package包/類
/**
* Returns a new JSON serializer, specialized for {@link SettingsManager}
* objects.
*
* @return The serializer to serialize {@code SettingsManager} objects with.
*/
static JsonSerializer<SettingsManager> newSettingsSerializer() {
return (settingsManager, typeOfObject, context) -> {
if (settingsManager == null)
return null;
Map<String, String> settings = settingsManager.getAllAsMap();
if (isEmpty(settings))
return null;
JsonObject jsonObject = new JsonObject();
for (Map.Entry<String, String> entry : settings.entrySet())
jsonObject.addProperty(entry.getKey(), entry.getValue());
return jsonObject;
};
}
示例5: newConfigurationSerializer
import com.google.gson.JsonSerializer; //導入依賴的package包/類
/**
* Returns a new JSON serializer, specialized for {@link Configuration}
* objects.
*
* @return The serializer to serialize {@code Configuration} objects with.
*/
static JsonSerializer<Configuration> newConfigurationSerializer() {
return (configuration, typeOfObject, context) -> {
if (configuration == null)
return null;
JsonObject jsonObject = new JsonObject();
HeaderManager defaultResponseHeaderManager = configuration.defaultResponseHeaderManager();
if (defaultResponseHeaderManager.keyCount() > 0)
jsonObject.add("defaultResponseHeaders", context.serialize(defaultResponseHeaderManager));
SettingsManager settingsManager = configuration.settingsManager();
if (settingsManager.entryCount() > 0)
jsonObject.add("settings", context.serialize(settingsManager));
List<MockRequest> requests = configuration.requests();
if (requests.size() > 0)
jsonObject.add("requests", context.serialize(requests));
return jsonObject;
};
}
示例6: newRequestSerializer
import com.google.gson.JsonSerializer; //導入依賴的package包/類
/**
* Returns a new JSON serializer, specialized for {@link MockRequest}
* objects.
*
* @return The serializer to serialize {@code MockRequest} objects with.
*/
static JsonSerializer<MockRequest> newRequestSerializer() {
return (request, typeOfObject, context) -> {
if (request == null)
return null;
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("url", request.url());
jsonObject.addProperty("method", request.method());
HeaderManager headerManager = request.headerManager();
if (headerManager.keyCount() > 0)
jsonObject.add("headers", context.serialize(headerManager));
SettingsManager settingsManager = request.settingsManager();
if (settingsManager.entryCount() > 0)
jsonObject.add("settings", context.serialize(settingsManager));
List<MockResponse> responses = request.responses();
if (notEmpty(responses))
jsonObject.add("responses", context.serialize(responses));
return jsonObject;
};
}
示例7: newResponseSerializer
import com.google.gson.JsonSerializer; //導入依賴的package包/類
/**
* Returns a new JSON serializer, specialized for {@link MockResponse}
* objects.
*
* @return The serializer to serialize {@code MockResponse} objects with.
*/
static JsonSerializer<MockResponse> newResponseSerializer() {
return (response, typeOfObject, context) -> {
if (response == null)
return null;
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("code", response.code());
jsonObject.addProperty("phrase", response.phrase());
String source = response.source();
if (notEmpty(source))
jsonObject.addProperty("source", source);
HeaderManager headerManager = response.headerManager();
if (headerManager.keyCount() > 0)
jsonObject.add("headers", context.serialize(headerManager));
SettingsManager settingsManager = response.settingsManager();
if (settingsManager.entryCount() > 0)
jsonObject.add("settings", context.serialize(settingsManager));
return jsonObject;
};
}
示例8: GSONJSONEngine
import com.google.gson.JsonSerializer; //導入依賴的package包/類
public GSONJSONEngine() {
this.serializer = new JsonSerializer<JSONSerializable>() {
public JsonElement serialize(JSONSerializable src, Type typeOfSrc, JsonSerializationContext context) {
Object o = src.toJSON(GSONJSONEngine.this);
if(o instanceof GSONJSONObject) return context.serialize(((GSONJSONObject) o).gsonObj);
else if(o instanceof GSONJSONArray) return context.serialize(((GSONJSONArray) o).gsonArr);
return context.serialize(o);
}
};
GsonBuilder builder = new GsonBuilder();
builder.registerTypeHierarchyAdapter(JSONSerializable.class, this.serializer);
if(Urmusic.DEBUG) builder.setPrettyPrinting();
this.gson = builder.create();
}
示例9: testCustomSerializationWithoutUsingMobileServiceTable
import com.google.gson.JsonSerializer; //導入依賴的package包/類
public void testCustomSerializationWithoutUsingMobileServiceTable() {
ComplexPersonTestObject person = new ComplexPersonTestObject("John", "Doe", new Address("1345 Washington St", 1313, "US"));
gsonBuilder.registerTypeAdapter(Address.class, new JsonSerializer<Address>() {
@Override
public JsonElement serialize(Address arg0, Type arg1, JsonSerializationContext arg2) {
JsonObject json = new JsonObject();
json.addProperty("zipcode", arg0.getZipCode());
json.addProperty("country", arg0.getCountry());
json.addProperty("streetaddress", arg0.getStreetAddress());
return json;
}
});
String serializedObject = gsonBuilder.create().toJson(person);
// Asserts
assertEquals(
"{\"address\":{\"zipcode\":1313,\"country\":\"US\",\"streetaddress\":\"1345 Washington St\"},\"firstName\":\"John\",\"id\":0,\"lastName\":\"Doe\"}",
serializedObject);
}
示例10: jsonToObject
import com.google.gson.JsonSerializer; //導入依賴的package包/類
public static <T> T jsonToObject(String jsonString, Class<T> classOfT) {
// Gson gson = new Gson();
Gson gson = new GsonBuilder()
.registerTypeHierarchyAdapter(Date.class,
new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src,
Type typeOfSrc,
JsonSerializationContext context) {
SimpleDateFormat format = new SimpleDateFormat(
dateformat);
return new JsonPrimitive(format.format(src));
}
}).setDateFormat(dateformat).create();
Object object = gson.fromJson(jsonString, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
示例11: testRegisteredSerializerOverridesJsonAdapter_add1167
import com.google.gson.JsonSerializer; //導入依賴的package包/類
/**
* The serializer overrides field adapter, but for deserializer the fieldAdapter is used.
*/
@Test(timeout = 1000)
public void testRegisteredSerializerOverridesJsonAdapter_add1167() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testRegisteredSerializerOverridesJsonAdapter_add1167");
JsonSerializer<A> serializer = new JsonSerializer<A>() {
public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("registeredSerializer");
}
};
Gson gson = new GsonBuilder().registerTypeAdapter(A.class, serializer).create();
String json = gson.toJson(new A("abcd"));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1677,json);
A target = gson.fromJson("abcd", A.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1678,target.value);
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例12: testRegisteredSerializerOverridesJsonAdapter_remove984
import com.google.gson.JsonSerializer; //導入依賴的package包/類
/**
* The serializer overrides field adapter, but for deserializer the fieldAdapter is used.
*/
@Test(timeout = 1000)
public void testRegisteredSerializerOverridesJsonAdapter_remove984() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testRegisteredSerializerOverridesJsonAdapter_remove984");
JsonSerializer<A> serializer = new JsonSerializer<A>() {
public JsonElement serialize(A src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive("registeredSerializer");
}
};
Gson gson = new GsonBuilder().registerTypeAdapter(A.class, serializer).create();
String json = gson.toJson(new A("abcd"));
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1677,json);
A target = gson.fromJson("abcd", A.class);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1678,target.value);
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例13: testCustomSerializers_add1030
import com.google.gson.JsonSerializer; //導入依賴的package包/類
@Test(timeout = 1000)
public void testCustomSerializers_add1030() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testCustomSerializers_add1030");
Gson gson = builder.registerTypeAdapter(TestTypes.ClassWithCustomTypeConverter.class, new JsonSerializer<com.google.gson.common.TestTypes.ClassWithCustomTypeConverter>() {
public JsonElement serialize(TestTypes.ClassWithCustomTypeConverter src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("bag", 5);
json.addProperty("bag", 5);
json.addProperty("value", 25);
return json;
}
}).create();
TestTypes.ClassWithCustomTypeConverter target = new TestTypes.ClassWithCustomTypeConverter();
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1145,gson,1144,gson.toJson(target));
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例14: testCustomSerializers_add1031
import com.google.gson.JsonSerializer; //導入依賴的package包/類
@Test(timeout = 1000)
public void testCustomSerializers_add1031() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testCustomSerializers_add1031");
Gson gson = builder.registerTypeAdapter(TestTypes.ClassWithCustomTypeConverter.class, new JsonSerializer<com.google.gson.common.TestTypes.ClassWithCustomTypeConverter>() {
public JsonElement serialize(TestTypes.ClassWithCustomTypeConverter src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("bag", 5);
json.addProperty("value", 25);
json.addProperty("value", 25);
return json;
}
}).create();
TestTypes.ClassWithCustomTypeConverter target = new TestTypes.ClassWithCustomTypeConverter();
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1145,gson,1144,gson.toJson(target));
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}
示例15: testCustomByteArraySerializer_add1022
import com.google.gson.JsonSerializer; //導入依賴的package包/類
@Test(timeout = 1000)
public void testCustomByteArraySerializer_add1022() {
fr.inria.diversify.testamplification.logger.Logger.writeTestStart(Thread.currentThread(),this, "testCustomByteArraySerializer_add1022");
Gson gson = new GsonBuilder().registerTypeAdapter(byte[].class, new JsonSerializer<byte[]>() {
public JsonElement serialize(byte[] src, Type typeOfSrc, JsonSerializationContext context) {
java.lang.StringBuilder sb = new java.lang.StringBuilder(src.length);
for (byte b : src) {
sb.append(b);
}
return new JsonPrimitive(sb.toString());
}
}).create();
byte[] data = new byte[]{ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };
String json = gson.toJson(data);
fr.inria.diversify.testamplification.logger.Logger.logAssertArgument(Thread.currentThread(),1127,json);
fr.inria.diversify.testamplification.logger.Logger.writeTestFinish(Thread.currentThread());
}