@Override public INDArray deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); if (node == null) return null; int size = node.size(); double[] d = new double[size]; for (int i = 0; i < size; i++) { d[i] = node.get(i).asDouble(); } return Nd4j.create(d); } }
@Override public INDArray deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException { JsonNode node = jp.getCodec().readTree(jp); JsonNode arr = node.get("dataBuffer"); int rank = node.get("rankField").asInt(); int numElements = node.get("numElements").asInt(); int offset = node.get("offsetField").asInt(); JsonNode shape = node.get("shapeField"); JsonNode stride = node.get("strideField"); String type = node.get("typeField").asText(); int[] realShape = new int[rank]; int[] realStride = new int[rank]; DataBuffer buff = Nd4j.createBuffer(numElements); for (int i = 0; i < numElements; i++) { buff.put(i, arr.get(i).asDouble()); } String ordering = node.get("orderingField").asText(); for (int i = 0; i < rank; i++) { realShape[i] = shape.get(i).asInt(); realStride[i] = stride.get(i).asInt(); } INDArray ret = type.equals("real") ? Nd4j.create(buff, realShape, realStride, offset, ordering.charAt(0)) : Nd4j.createComplex(buff, realShape, realStride, offset, ordering.charAt(0)); return ret; } }
/** * 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; } }
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; }
/** * 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); }
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; }
/** * @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() { ObjectMapper ret = new ObjectMapper(); configureMapper(ret); return ret; }
@Override public AtomicDouble deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); double value = node.asDouble(); return new AtomicDouble(value); } }
/** * Reinitialize and return the Jackson/json ObjectMapper with additional named types. * This can be used to add additional subtypes at runtime (i.e., for JSON mapping with * types defined outside of the main DL4J codebase) */ public static ObjectMapper reinitMapperWithSubtypes(Collection<NamedType> additionalTypes) { mapper.registerSubtypes(additionalTypes.toArray(new NamedType[additionalTypes.size()])); //Recreate the mapper (via copy), as mapper won't use registered subtypes after first use mapper = mapper.copy(); return mapper; }
@Override public AtomicBoolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { JsonNode node = jsonParser.getCodec().readTree(jsonParser); boolean value = node.asBoolean(); return new AtomicBoolean(value); } }
/** * Convenience function for parsing JSON strings. * * @param json String containing valid JSON * @return Nested (key,value) map of arbitrary depth * @throws IOException */ public static Map<String, Object> parseJsonString(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {}; return mapper.readValue(json, typeRef); }
/** * @param yaml YAML representation * @param clazz Class * @param <T> Type to return * @return Evaluation instance */ public static <T extends IEvaluation> T fromYaml(String yaml, Class<T> clazz) { try { return yamlMapper.readValue(yaml, clazz); } catch (IOException e) { throw new RuntimeException(e); } }
/** * @return JSON representation of the evaluation instance */ @Override public String toJson() { try { return objectMapper.writeValueAsString(this); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
/** * Convenience function for parsing JSON strings. * * @param json String containing valid JSON * @return Nested (key,value) map of arbitrary depth * @throws IOException */ public static Map<String, Object> parseYamlString(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {}; return mapper.readValue(json, typeRef); }
/** * @param json Jason representation of the evaluation instance * @param clazz Class * @param <T> Type to return * @return Evaluation instance */ public static <T extends IEvaluation> T fromJson(String json, Class<T> clazz) { try { return objectMapper.readValue(json, clazz); } catch (IOException e) { throw new RuntimeException(e); } }
/** * @return JSON representation of the curve */ public String toJson() { try { return BaseEvaluation.getObjectMapper().writeValueAsString(this); } catch (JsonProcessingException e) { throw new RuntimeException(e); } }
public static MemoryReport fromJson(String json) { try { return NeuralNetConfiguration.mapper().readValue(json, MemoryReport.class); } catch (IOException e) { throw new RuntimeException(e); } }