@Override public DateSerializer withFormat(Boolean timestamp, DateFormat customFormat) { return new DateSerializer(timestamp, customFormat); }
@Override public void serialize(Date value, JsonGenerator g, SerializerProvider provider) throws IOException { if (_asTimestamp(provider)) { g.writeNumber(_timestamp(value)); return; } _serializeAsString(value, g, provider); } }
@Override public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_asTimestamp(provider)) { gen.writeNumber(_timestamp(value)); } else if (_customFormat != null) { // 21-Feb-2011, tatu: not optimal, but better than alternatives: synchronized (_customFormat) { gen.writeString(_customFormat.format(value)); } } else { provider.defaultSerializeDateValue(value, gen); } } }
@Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_useTimestamp) { jgen.writeNumber(_timestamp(value)); } else if (_customFormat != null) { // 21-Feb-2011, tatu: not optimal, but better than alternatives: synchronized (_customFormat) { jgen.writeString(_customFormat.format(value)); } } else { provider.defaultSerializeDateValue(value, jgen); } } }
@Override public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_asTimestamp(provider)) { gen.writeNumber(_timestamp(value)); } else if (_customFormat != null) { // 21-Feb-2011, tatu: not optimal, but better than alternatives: synchronized (_customFormat) { gen.writeString(_customFormat.format(value)); } } else { provider.defaultSerializeDateValue(value, gen); } } }
@Override public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { if (_useTimestamp) { jgen.writeNumber(_timestamp(value)); } else if (_customFormat != null) { // 21-Feb-2011, tatu: not optimal, but better than alternatives: synchronized (_customFormat) { jgen.writeString(_customFormat.format(value)); } } else { provider.defaultSerializeDateValue(value, jgen); } } }
public class JSONMapper extends ObjectMapper { public JSONMapper() { SimpleModule module = new SimpleModule("JSONModule", new Version(2, 0, 0, null, null, null)); module.addSerializer(Date.class, new DateSerializer()); module.addDeserializer(Date.class, new DateDeserializer()); // Add more here ... registerModule(module); } }
@Override public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException { if (_asTimestamp(provider)) { gen.writeNumber(_timestamp(value)); } else if (_customFormat != null) { // 21-Feb-2011, tatu: not optimal, but better than alternatives: synchronized (_customFormat) { gen.writeString(_customFormat.format(value)); } } else { provider.defaultSerializeDateValue(value, gen); } } }
@Override public void serialize(Date value, JsonGenerator g, SerializerProvider provider) throws IOException { if (_asTimestamp(provider)) { g.writeNumber(_timestamp(value)); return; } _serializeAsString(value, g, provider); } }
@Override public DateSerializer withFormat(boolean timestamp, DateFormat customFormat) { if (timestamp) { return new DateSerializer(true, null); } return new DateSerializer(false, customFormat); }
@Override public DateSerializer withFormat(boolean timestamp, DateFormat customFormat) { if (timestamp) { return new DateSerializer(true, null); } return new DateSerializer(false, customFormat); }
@Override public DateSerializer withFormat(Boolean timestamp, DateFormat customFormat) { return new DateSerializer(timestamp, customFormat); }
@Override public DateSerializer withFormat(Boolean timestamp, DateFormat customFormat) { return new DateSerializer(timestamp, customFormat); }
@Override public DateSerializer withFormat(Boolean timestamp, DateFormat customFormat) { return new DateSerializer(timestamp, customFormat); }
@Override public DateSerializer withFormat(Boolean timestamp, DateFormat customFormat) { return new DateSerializer(timestamp, customFormat); }
public static JSONObject Object(Object o){ try { GsonBuilder gsonb = new GsonBuilder(); DateSerializer ds = new DateSerializer(); gsonb.registerTypeAdapter(Date.class, ds); Gson gson = gsonb.create(); return new JSONObject(gson.toJson(o)); } catch (JSONException e) { e.printStackTrace(); } return null; }
ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Date.class, new DateSerializer()); mapper.registerModule(module);
@Provider public class JacksonConfig implements ContextResolver<ObjectMapper> { private ObjectMapper objectMapper; public JacksonConfig() { objectMapper = new ObjectMapper(); SimpleModule module = new SimpleModule("MyModule", new Version(1, 0, 0, null)); module.addSerializer(Money.class, new MoneySerializer()); module.addSerializer(LocalDate.class, new DateSerializer()); objectMapper.registerModule(module); } public ObjectMapper getContext(Class<?> objectType) { return objectMapper; } }
Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, new DateDeserializer()) .registerTypeAdapter(Date.class, new DateSerializer()) .create();
private Client buildClient() { return ClientBuilder.newBuilder() .register(getJacksonJsonProvider()) .build(); } private JacksonJsonProvider getJacksonJsonProvider() { JacksonJsonProvider jjp = new JacksonJaxbJsonProvider(); jjp.setMapper(getJsonObjectMapper()); return jjp; } private ObjectMapper getJsonObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); SimpleModule module = new SimpleModule(); module.addSerializer(java.sql.Date.class, new DateSerializer()); // <-- My Fix mapper.registerModule(module); return mapper; }