本文整理汇总了Java中com.google.gson.GsonBuilder.setDateFormat方法的典型用法代码示例。如果您正苦于以下问题:Java GsonBuilder.setDateFormat方法的具体用法?Java GsonBuilder.setDateFormat怎么用?Java GsonBuilder.setDateFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.gson.GsonBuilder
的用法示例。
在下文中一共展示了GsonBuilder.setDateFormat方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: EventServlet
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public EventServlet(LeshanServer server, int securePort) {
server.getRegistrationService().addListener(this.registrationListener);
server.getObservationService().addListener(this.observationListener);
// add an interceptor to each endpoint to trace all CoAP messages
coapMessageTracer = new CoapMessageTracer(server.getRegistrationService());
for (Endpoint endpoint : server.getCoapServer().getEndpoints()) {
endpoint.addInterceptor(coapMessageTracer);
}
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeHierarchyAdapter(Registration.class, new RegistrationSerializer(securePort));
gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
this.gson = gsonBuilder.create();
}
示例2: getGsonBuilder
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
/**
* 构建通用GsonBuilder, 封装初始化工作
*
* @return
*/
public static GsonBuilder getGsonBuilder(boolean prettyPrinting) {
GsonBuilder gb = new GsonBuilder();
gb.setDateFormat("yyyy-MM-dd HH:mm:ss:mss");
gb.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(WJsonExclued.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return clazz.getAnnotation(WJsonExclued.class) != null;
}
});
if (prettyPrinting)
gb.setPrettyPrinting();
return gb;
}
示例3: writeTo
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
@Override
public void writeTo(Representation rep, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException,
WebApplicationException {
try (OutputStreamWriter writer = new OutputStreamWriter(entityStream,
CommonParams.CHARSET)) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat(CommonParams.FORMAT_DATE);
if (rep.getVersion() != null) {
builder.setVersion(rep.getVersion());
}
Gson gson = builder.create();
gson.toJson(rep, genericType, writer);
} catch (JsonSyntaxException e) {
throw WebException.internalServerError()
.message(CommonParams.ERROR_JSON_FORMAT).build();
}
}
示例4: createGson
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static Gson createGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
//gsonBuilder.setExclusionStrategies(new SpecificClassExclusionStrategy(null, Model.class));
gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss");
JsonDeserializer deserializer = new IntegerJsonDeserializer();
gsonBuilder.registerTypeAdapter(int.class, deserializer);
gsonBuilder.registerTypeAdapter(Integer.class, deserializer);
deserializer = new FloatJsonDeserializer();
gsonBuilder.registerTypeAdapter(float.class, deserializer);
gsonBuilder.registerTypeAdapter(Float.class, deserializer);
deserializer = new DoubleJsonDeserializer();
gsonBuilder.registerTypeAdapter(double.class, deserializer);
gsonBuilder.registerTypeAdapter(Double.class, deserializer);
deserializer = new StringJsonDeserializer();
gsonBuilder.registerTypeAdapter(String.class, deserializer);
gsonBuilder.registerTypeAdapter(Tweet.Image.class, new ImageJsonDeserializer());
return gsonBuilder.create();
}
示例5: toJson
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static String toJson(Object object) {
if (gson == null) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
gson = builder.create();
}
String json = gson.toJson(object);
return json;
}
示例6: jsonToBean
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static <T> T jsonToBean(String json, Class<T> class1) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("yyyy-MM-dd HH:mm:ss");
Gson gson = builder.create();
T t = gson.fromJson(json, class1);
return t;
}
示例7: ClientServlet
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public ClientServlet(LwM2mServer server, int securePort) {
this.server = server;
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeHierarchyAdapter(Registration.class, new RegistrationSerializer(securePort));
gsonBuilder.registerTypeHierarchyAdapter(LwM2mResponse.class, new ResponseSerializer());
gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeSerializer());
gsonBuilder.registerTypeHierarchyAdapter(LwM2mNode.class, new LwM2mNodeDeserializer());
gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
this.gson = gsonBuilder.create();
}
示例8: DataSerializer
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public DataSerializer(Class<?> clazz) {
this.clazz = clazz;
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat(FORMAT_DATE);
this.gson = builder.create();
}
示例9: buildGson
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static Gson buildGson() {
// Creates the json object which will manage the information received
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat(DateUtils.SERVER_DATE_TIME_PATTERN);
// Register an adapter to manage the date types as long values
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
return builder.create();
}
示例10: toArrayList
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public ArrayList<T> toArrayList(String jsonString, Class<T> clazz) {
GsonBuilder builder = new GsonBuilder();
builder.setDateFormat("dd/MM/yy HH:mm:ss");
Gson gson = builder.create();
ListParameterizedType type = new ListParameterizedType(clazz);
ArrayList list = (ArrayList)gson.fromJson(jsonString, type);
return list;
}
示例11: create
import com.google.gson.GsonBuilder; //导入方法依赖的package包/类
public static final Gson create(boolean pretty) {
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
builder.disableHtmlEscaping();
// Install MongoDB / BSON serializers
tryToAddSerializers("io.datatree.dom.adapters.JsonGsonBsonSerializers", builder);
// Install serializers for Apache Cassandra
addSerializer(builder, InetAddress.class, (value) -> {
return new JsonPrimitive(value.getCanonicalHostName());
});
addSerializer(builder, Inet4Address.class, (value) -> {
return new JsonPrimitive(value.getCanonicalHostName());
});
addSerializer(builder, Inet6Address.class, (value) -> {
return new JsonPrimitive(value.getCanonicalHostName());
});
// Date serializer
if (Config.USE_TIMESTAMPS) {
builder.setDateFormat(Config.TIMESTAMP_FORMAT);
} else {
// Milliseconds since epoch Jan 1 , 1970 00:00:00 UTC
addSerializer(builder, Date.class, (value) -> {
return new JsonPrimitive(value.getTime());
});
}
// BASE64 serializer
addSerializer(builder, byte[].class, (value) -> {
return new JsonPrimitive(BASE64.encode(value));
});
// Pretty printing
if (pretty) {
builder.setPrettyPrinting();
}
return builder.create();
}