Search in sources :

Example 6 with ISO8601DateFormat

use of com.fasterxml.jackson.databind.util.ISO8601DateFormat in project graphhopper by graphhopper.

the class GraphHopperServletModule method createObjectMapper.

@Provides
@Singleton
ObjectMapper createObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new ISO8601DateFormat());
    objectMapper.registerModule(new JtsModule());
    SimpleModule pathDetailModule = new SimpleModule();
    pathDetailModule.addSerializer(PathDetail.class, new PathDetailSerializer());
    pathDetailModule.addDeserializer(PathDetail.class, new PathDetailDeserializer());
    objectMapper.registerModule(pathDetailModule);
    // Because VirtualEdgeIteratorState has getters which throw Exceptions.
    // http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions
    objectMapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {

        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {

                @Override
                public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
                    try {
                        super.serializeAsField(bean, gen, prov);
                    } catch (Exception e) {
                    // Ignoring expected exception, see above.
                    }
                }
            }).collect(Collectors.toList());
        }
    }));
    return objectMapper;
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) CmdArgs(com.graphhopper.util.CmdArgs) IOException(java.io.IOException) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ServletModule(com.google.inject.servlet.ServletModule) Collectors(java.util.stream.Collectors) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) List(java.util.List) Provides(com.google.inject.Provides) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) Map(java.util.Map) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) com.fasterxml.jackson.databind(com.fasterxml.jackson.databind) PathDetail(com.graphhopper.util.details.PathDetail) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) List(java.util.List) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 7 with ISO8601DateFormat

use of com.fasterxml.jackson.databind.util.ISO8601DateFormat in project jackson-databind by FasterXML.

the class DateDeserializationTest method testISO8601Directly.

// Based on an external report; was throwing an exception for second case here
public void testISO8601Directly() throws Exception {
    final String TIME_STR = "2015-01-21T08:56:13.533+0000";
    Date d = MAPPER.readValue(quote(TIME_STR), Date.class);
    assertNotNull(d);
    ISO8601DateFormat f = new ISO8601DateFormat();
    Date d2 = f.parse(TIME_STR);
    assertNotNull(d2);
    assertEquals(d.getTime(), d2.getTime());
}
Also used : ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) Date(java.util.Date)

Example 8 with ISO8601DateFormat

use of com.fasterxml.jackson.databind.util.ISO8601DateFormat in project JMRI by JMRI.

the class JsonUtil method getTime.

@Deprecated
public static JsonNode getTime(Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, TIME);
    ObjectNode data = root.putObject(DATA);
    data.put(TIME, new ISO8601DateFormat().format(InstanceManager.getDefault(jmri.Timebase.class).getTime()));
    data.put(RATE, InstanceManager.getDefault(jmri.Timebase.class).getRate());
    data.put(STATE, InstanceManager.getDefault(jmri.Timebase.class).getRun() ? ON : OFF);
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat)

Example 9 with ISO8601DateFormat

use of com.fasterxml.jackson.databind.util.ISO8601DateFormat in project JMRI by JMRI.

the class JsonRosterHttpService method getRosterEntry.

/**
     * Returns the JSON representation of a roster entry.
     *
     * Note that this returns, for images and icons, a URL relative to the root
     * folder of the JMRI server. It is expected that clients will fill in the
     * server IP address and port as they know it to be.
     *
     * @param locale the client's Locale
     * @param entry  A RosterEntry that may or may not be in the roster.
     * @return a roster entry in JSON notation
     */
public JsonNode getRosterEntry(Locale locale, @Nonnull RosterEntry entry) {
    ObjectNode root = this.mapper.createObjectNode();
    root.put(TYPE, JsonRoster.ROSTER_ENTRY);
    ObjectNode data = root.putObject(DATA);
    data.put(NAME, entry.getId());
    data.put(ADDRESS, entry.getDccAddress());
    data.put(IS_LONG_ADDRESS, entry.isLongAddress());
    data.put(ROAD, entry.getRoadName());
    data.put(NUMBER, entry.getRoadNumber());
    data.put(MFG, entry.getMfg());
    data.put(DECODER_MODEL, entry.getDecoderModel());
    data.put(DECODER_FAMILY, entry.getDecoderFamily());
    data.put(MODEL, entry.getModel());
    data.put(COMMENT, entry.getComment());
    data.put(MAX_SPD_PCT, Integer.toString(entry.getMaxSpeedPCT()));
    data.put(IMAGE, (entry.getImagePath() != null) ? "/" + JsonRoster.ROSTER + "/" + entry.getId() + "/" + IMAGE : null);
    data.put(ICON, (entry.getIconPath() != null) ? "/" + JsonRoster.ROSTER + "/" + entry.getId() + "/" + ICON : null);
    data.put(SHUNTING_FUNCTION, entry.getShuntingFunction());
    data.put(JsonRoster.DATE_MODIFIED, (entry.getDateModified() != null) ? new ISO8601DateFormat().format(entry.getDateModified()) : null);
    ArrayNode labels = data.putArray(FUNCTION_KEYS);
    for (int i = 0; i <= entry.getMAXFNNUM(); i++) {
        ObjectNode label = mapper.createObjectNode();
        label.put(NAME, F + i);
        label.put(LABEL, entry.getFunctionLabel(i));
        label.put(LOCKABLE, entry.getFunctionLockable(i));
        label.put(ICON, (entry.getFunctionImage(i) != null) ? "/" + JsonRoster.ROSTER + "/" + entry.getId() + "/" + F + i + "/" + ICON : null);
        label.put(SELECTED_ICON, (entry.getFunctionSelectedImage(i) != null) ? "/" + JsonRoster.ROSTER + "/" + entry.getId() + "/" + F + i + "/" + SELECTED_ICON : null);
        labels.add(label);
    }
    ArrayNode attributes = data.putArray(JsonRoster.ATTRIBUTES);
    entry.getAttributes().stream().forEach((name) -> {
        ObjectNode attribute = mapper.createObjectNode();
        attribute.put(NAME, name);
        attribute.put(VALUE, entry.getAttribute(name));
        attributes.add(attribute);
    });
    ArrayNode rga = data.putArray(JsonRoster.ROSTER_GROUPS);
    entry.getGroups().stream().forEach((group) -> {
        rga.add(group.getName());
    });
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 10 with ISO8601DateFormat

use of com.fasterxml.jackson.databind.util.ISO8601DateFormat in project JMRI by JMRI.

the class JsonTimeHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = this.mapper.createObjectNode();
    root.put(TYPE, TIME);
    ObjectNode data = root.putObject(DATA);
    data.put(TIME, new ISO8601DateFormat().format(InstanceManager.getDefault(jmri.Timebase.class).getTime()));
    data.put(RATE, InstanceManager.getDefault(jmri.Timebase.class).getRate());
    data.put(STATE, InstanceManager.getDefault(jmri.Timebase.class).getRun() ? ON : OFF);
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat)

Aggregations

ISO8601DateFormat (com.fasterxml.jackson.databind.util.ISO8601DateFormat)13 DispatcherType (javax.servlet.DispatcherType)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 JSONSerializer (com.alibaba.fastjson.serializer.JSONSerializer)1 SerializeConfig (com.alibaba.fastjson.serializer.SerializeConfig)1 JtsModule (com.bedatadriven.jackson.datatype.jts.JtsModule)1 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 com.fasterxml.jackson.databind (com.fasterxml.jackson.databind)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 BeanPropertyWriter (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)1 BeanSerializerModifier (com.fasterxml.jackson.databind.ser.BeanSerializerModifier)1 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)1 Provides (com.google.inject.Provides)1 ServletModule (com.google.inject.servlet.ServletModule)1 CmdArgs (com.graphhopper.util.CmdArgs)1 PathDetail (com.graphhopper.util.details.PathDetail)1