T t = m.readValue(nodeAsString, lClass); return t; } catch (Throwable e){
/** * Get the onnx op descriptors by name * @return the onnx op descriptors by name * @throws Exception */ public static Map<String,OpDescriptor> onnxOpDescriptors() throws Exception { try(InputStream is = new ClassPathResource("onnxops.json").getInputStream()) { ObjectMapper objectMapper = new ObjectMapper(); OnnxDescriptor opDescriptor = objectMapper.readValue(is,OnnxDescriptor.class); Map<String,OpDescriptor> descriptorMap = new HashMap<>(); for(OpDescriptor descriptor : opDescriptor.getDescriptors()) { descriptorMap.put(descriptor.getName(),descriptor); } return descriptorMap; } }
/** * @return YAML representation of the evaluation instance */ @Override public String toYaml() { try { return yamlMapper.writeValueAsString(this); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
private static ObjectMapper initMapper(JsonFactory factory) { ObjectMapper om = new ObjectMapper(factory); om.registerModule(new JodaModule()); om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); om.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); om.enable(SerializationFeature.INDENT_OUTPUT); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE); om.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); return om; }
public static String renderHTMLContent(Component... components) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); mapper.enable(SerializationFeature.INDENT_OUTPUT); Configuration cfg = new Configuration(new Version(2, 3, 23)); // Where do we load the templates from: cfg.setClassForTemplateLoading(StaticPageUtil.class, ""); // Some other recommended settings: cfg.setIncompatibleImprovements(new Version(2, 3, 23)); cfg.setDefaultEncoding("UTF-8"); cfg.setLocale(Locale.US); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); ClassPathResource cpr = new ClassPathResource("assets/dl4j-ui.js"); String scriptContents = IOUtils.toString(cpr.getInputStream(), "UTF-8"); Map<String, Object> pageElements = new HashMap<>(); List<ComponentObject> list = new ArrayList<>(); int i = 0; for (Component c : components) { list.add(new ComponentObject(String.valueOf(i), mapper.writeValueAsString(c))); i++; } pageElements.put("components", list); pageElements.put("scriptcontent", scriptContents); Template template = cfg.getTemplate("staticpage.ftl"); Writer stringWriter = new StringWriter(); template.process(pageElements, stringWriter); return stringWriter.toString(); }
private static ObjectMapper getMapper(){ ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); return mapper; }
attrPointer.get(attrBuffer); s = new String(attrBuffer); ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY); try { mapper.readTree(s); break; } catch (IOException e) {
private static ObjectMapper initMapper() { ObjectMapper ret = new ObjectMapper(); configureMapper(ret); return ret; }
private static ObjectMapper configureMapper(ObjectMapper ret) { ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, false); ret.enable(SerializationFeature.INDENT_OUTPUT); SimpleModule atomicModule = new SimpleModule(); atomicModule.addSerializer(AtomicDouble.class,new JsonSerializerAtomicDouble()); atomicModule.addSerializer(AtomicBoolean.class,new JsonSerializerAtomicBoolean()); atomicModule.addDeserializer(AtomicDouble.class,new JsonDeserializerAtomicDouble()); atomicModule.addDeserializer(AtomicBoolean.class,new JsonDeserializerAtomicBoolean()); ret.registerModule(atomicModule); //Serialize fields only, not using getters ret.setVisibilityChecker(ret.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); return ret; }
@Override public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonNode node = p.getCodec().readTree(p); String className = node.get("@class").asText(); Class<?> c; try { c = Class.forName(className); } catch (Exception e) { throw new RuntimeException(e); } JsonNode valueNode = node.get("value"); Object o = new ObjectMapper().treeToValue(valueNode, c); return o; } }
@Test public void jsonSaverCreatesExpectedJsonOutput() throws IOException { URL url = Resources.getResource(JSON_EXPECTED_OUTPUT_FILE); String expectedOutput = Resources.toString(url, Charsets.UTF_8); ObjectMapper mapper = new ObjectMapper(); JsonNode expectedJsonObject = mapper.readTree(expectedOutput); JsonNode actualJsonObject = mapper.readTree(outputStream.toString()); assertEquals(expectedJsonObject, actualJsonObject); }
private static void configureMapper(ObjectMapper ret) { ret.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ret.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); ret.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); ret.enable(SerializationFeature.INDENT_OUTPUT); SimpleModule customDeserializerModule = new SimpleModule(); customDeserializerModule.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { //Use our custom deserializers to handle backward compatibility for updaters -> IUpdater if (beanDesc.getBeanClass() == MultiLayerConfiguration.class) { return new MultiLayerConfigurationDeserializer(deserializer); } else if (beanDesc.getBeanClass() == ComputationGraphConfiguration.class) { return new ComputationGraphConfigurationDeserializer(deserializer); } return deserializer; } }); ret.registerModule(customDeserializerModule); registerSubtypes(ret); }
AnnotatedClass ac = AnnotatedClass.construct(c, mapper.getSerializationConfig().getAnnotationIntrospector(), null); Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector()); for (NamedType nt : types) { registeredSubtypes.add(nt.getType()); mapper.registerSubtypes(toRegister.toArray(new NamedType[toRegister.size()])); mapper = mapper.copy(); return mapper;
AnnotatedClass ac = AnnotatedClass.construct(c, mapper.getSerializationConfig().getAnnotationIntrospector(), null); Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector()); for (NamedType nt : types) { registeredSubtypes.add(nt.getType()); mapper.registerSubtypes(toRegister.toArray(new NamedType[toRegister.size()]));
/** * @return JSON representation of the evaluation instance */ @Override public String toJson() { try { return objectMapper.writeValueAsString(this); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
ObjectMapper mapper = NeuralNetConfiguration.mapper(); try { conf = mapper.readValue(json, MultiLayerConfiguration.class); } catch (IOException e) { throw new RuntimeException(e); JsonNode jsonNode = mapper.readTree(json); if (confs == null) { confs = jsonNode.get("confs"); JsonNode jsonNode = mapper.readTree(json); if (confs == null) { confs = jsonNode.get("confs");
/** * * @param types * @param json * @return */ public static ObjectMapper findTypesFor(List<Class<?>> types, boolean json) { return withTypes(json ? new ObjectMapper() : new ObjectMapper(new YAMLFactory()), getImpls(types)); }
@Override public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { JsonNode node = p.getCodec().readTree(p); String className = node.get("class").asText(); Class<?> c; try { c = Class.forName(className); } catch (Exception e) { throw new RuntimeException(e); } JsonNode valueNode = node.get("value"); Object o = new ObjectMapper().treeToValue(valueNode, c); return o; } }
/** * Instantiate the given class type * @param clazz the class to instantiate * @param json the json to instantiate from * @param objectMapper the object mapper to * @param <T> * @return * @throws IOException */ public static <T> T instantiateType(Class<T> clazz, String json, ObjectMapper objectMapper) throws IOException { return objectMapper.readValue(json, clazz); }