org.codehaus.jackson.map.SerializationConfig Java Examples
The following examples show how to use
org.codehaus.jackson.map.SerializationConfig.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Jackson1Parser.java From typescript-generator with MIT License | 6 votes |
private BeanHelper getBeanHelper(Class<?> beanClass) { if (beanClass == null) { return null; } try { final SerializationConfig serializationConfig = objectMapper.getSerializationConfig(); final JavaType simpleType = objectMapper.constructType(beanClass); final JsonSerializer<?> jsonSerializer = BeanSerializerFactory.instance.createSerializer(serializationConfig, simpleType, null); if (jsonSerializer == null) { return null; } if (jsonSerializer instanceof BeanSerializer) { return new BeanHelper((BeanSerializer) jsonSerializer); } else { final String jsonSerializerName = jsonSerializer.getClass().getName(); throw new RuntimeException(String.format("Unknown serializer '%s' for class '%s'", jsonSerializerName, beanClass)); } } catch (JsonMappingException e) { throw new RuntimeException(e); } }
Example #2
Source File: JsonTopologySerializer.java From libcrunch with Apache License 2.0 | 6 votes |
public void setupModule(SetupContext context) { context.addBeanSerializerModifier(new BeanSerializerModifier() { @Override public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BasicBeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) { ListIterator<BeanPropertyWriter> it = beanProperties.listIterator(); while (it.hasNext()) { BeanPropertyWriter writer = it.next(); // replace the bean writer with my own if it is for "failed" if (writer.getName().equals("failed")) { BeanPropertyWriter newWriter = new IsFailedWriter(writer); it.set(newWriter); } } return beanProperties; } }); }
Example #3
Source File: TestZNRecordSerializeWriteSizeLimit.java From helix with Apache License 2.0 | 6 votes |
private byte[] serialize(Object data) { ObjectMapper mapper = new ObjectMapper(); SerializationConfig serializationConfig = mapper.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true); serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] serializedBytes = new byte[0]; try { mapper.writeValue(baos, data); serializedBytes = baos.toByteArray(); } catch (IOException e) { Assert.fail("Can not serialize data.", e); } return serializedBytes; }
Example #4
Source File: JacksonPayloadSerializer.java From helix with Apache License 2.0 | 6 votes |
@Override public <T> byte[] serialize(final T data) { if (data == null) { return null; } ObjectMapper mapper = new ObjectMapper(); SerializationConfig serializationConfig = mapper.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true); serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); StringWriter sw = new StringWriter(); try { mapper.writeValue(sw, data); } catch (Exception e) { logger.error("Exception during payload data serialization.", e); throw new ZkClientException(e); } return sw.toString().getBytes(); }
Example #5
Source File: PropertyJsonSerializer.java From helix with Apache License 2.0 | 6 votes |
@Override public byte[] serialize(T data) throws PropertyStoreException { ObjectMapper mapper = new ObjectMapper(); SerializationConfig serializationConfig = mapper.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); serializationConfig.set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, true); serializationConfig.set(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); StringWriter sw = new StringWriter(); try { mapper.writeValue(sw, data); if (sw.toString().getBytes().length > ZNRecord.SIZE_LIMIT) { throw new HelixException("Data size larger than 1M. Write empty string to zk."); } return sw.toString().getBytes(); } catch (Exception e) { LOG.error("Error during serialization of data (first 1k): " + sw.toString().substring(0, 1024), e); } return new byte[] {}; }
Example #6
Source File: StatePool.java From big-c with Apache License 2.0 | 6 votes |
private void write(DataOutput out) throws IOException { // This is just a JSON experiment System.out.println("Dumping the StatePool's in JSON format."); ObjectMapper outMapper = new ObjectMapper(); outMapper.configure( SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); // define a module SimpleModule module = new SimpleModule("State Serializer", new Version(0, 1, 1, "FINAL")); // add the state serializer //module.addSerializer(State.class, new StateSerializer()); // register the module with the object-mapper outMapper.registerModule(module); JsonFactory outFactory = outMapper.getJsonFactory(); JsonGenerator jGen = outFactory.createJsonGenerator((DataOutputStream)out, JsonEncoding.UTF8); jGen.useDefaultPrettyPrinter(); jGen.writeObject(this); jGen.close(); }
Example #7
Source File: JsonObjectMapperWriter.java From big-c with Apache License 2.0 | 6 votes |
public JsonObjectMapperWriter(OutputStream output, boolean prettyPrint) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure( SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); // define a module SimpleModule module = new SimpleModule("Default Serializer", new Version(0, 1, 1, "FINAL")); // add various serializers to the module // add default (all-pass) serializer for all rumen specific data types module.addSerializer(DataType.class, new DefaultRumenSerializer()); // add a serializer to use object.toString() while serializing module.addSerializer(ID.class, new ObjectStringSerializer<ID>()); // register the module with the object-mapper mapper.registerModule(module); mapper.getJsonFactory(); writer = mapper.getJsonFactory().createJsonGenerator( output, JsonEncoding.UTF8); if (prettyPrint) { writer.useDefaultPrettyPrinter(); } }
Example #8
Source File: WriteJsonServletHandler.java From uflo with Apache License 2.0 | 6 votes |
protected void writeObjectToJson(HttpServletResponse resp,Object obj) throws ServletException, IOException{ resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setContentType("text/json"); resp.setCharacterEncoding("UTF-8"); ObjectMapper mapper=new ObjectMapper(); mapper.setSerializationInclusion(Inclusion.NON_NULL); mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); OutputStream out = resp.getOutputStream(); try { mapper.writeValue(out, obj); } finally { out.flush(); out.close(); } }
Example #9
Source File: StatePool.java From hadoop with Apache License 2.0 | 6 votes |
private void write(DataOutput out) throws IOException { // This is just a JSON experiment System.out.println("Dumping the StatePool's in JSON format."); ObjectMapper outMapper = new ObjectMapper(); outMapper.configure( SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); // define a module SimpleModule module = new SimpleModule("State Serializer", new Version(0, 1, 1, "FINAL")); // add the state serializer //module.addSerializer(State.class, new StateSerializer()); // register the module with the object-mapper outMapper.registerModule(module); JsonFactory outFactory = outMapper.getJsonFactory(); JsonGenerator jGen = outFactory.createJsonGenerator((DataOutputStream)out, JsonEncoding.UTF8); jGen.useDefaultPrettyPrinter(); jGen.writeObject(this); jGen.close(); }
Example #10
Source File: WriteJsonServletHandler.java From urule with Apache License 2.0 | 6 votes |
protected void writeObjectToJson(HttpServletResponse resp,Object obj) throws ServletException, IOException{ resp.setHeader("Access-Control-Allow-Origin", "*"); resp.setContentType("text/json"); resp.setCharacterEncoding("UTF-8"); ObjectMapper mapper=new ObjectMapper(); mapper.setSerializationInclusion(Inclusion.NON_NULL); mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false); mapper.setDateFormat(new SimpleDateFormat(Configure.getDateFormat())); OutputStream out = resp.getOutputStream(); try { mapper.writeValue(out, obj); } finally { out.flush(); out.close(); } }
Example #11
Source File: JsonObjectMapperWriter.java From hadoop with Apache License 2.0 | 6 votes |
public JsonObjectMapperWriter(OutputStream output, boolean prettyPrint) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.configure( SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true); // define a module SimpleModule module = new SimpleModule("Default Serializer", new Version(0, 1, 1, "FINAL")); // add various serializers to the module // add default (all-pass) serializer for all rumen specific data types module.addSerializer(DataType.class, new DefaultRumenSerializer()); // add a serializer to use object.toString() while serializing module.addSerializer(ID.class, new ObjectStringSerializer<ID>()); // register the module with the object-mapper mapper.registerModule(module); mapper.getJsonFactory(); writer = mapper.getJsonFactory().createJsonGenerator( output, JsonEncoding.UTF8); if (prettyPrint) { writer.useDefaultPrettyPrinter(); } }
Example #12
Source File: AbstractResource.java From helix with Apache License 2.0 | 5 votes |
protected static String toJson(Object object) throws IOException { SerializationConfig serializationConfig = OBJECT_MAPPER.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); StringWriter sw = new StringWriter(); OBJECT_MAPPER.writeValue(sw, object); sw.append('\n'); return sw.toString(); }
Example #13
Source File: Json.java From ambari-metrics with Apache License 2.0 | 5 votes |
/** * Creates a Json ObjectMapper that maps fields and optionally pretty prints the * serialized objects. * * @param pretty a flag - if true the output will be pretty printed. */ public Json(boolean pretty) { myObjectMapper = new ObjectMapper(); myObjectMapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY); if (pretty) { myObjectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); } }
Example #14
Source File: ObjectXMLWriter.java From secure-data-service with Apache License 2.0 | 5 votes |
@Override public void writeTo(Object o, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { XmlMapper xmlMapper = new XmlMapper(); xmlMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); xmlMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); xmlMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); xmlMapper.writeValue(entityStream, o); }
Example #15
Source File: TestJsonSerde.java From hraven with Apache License 2.0 | 5 votes |
@Test public void testJsonSerializationFlowStatsJobDetails() throws Exception { // load a sample flow final short numJobsAppOne = 3; final short numJobsAppTwo = 4; final long baseStats = 10L; Table historyTable = hbaseConnection.getTable(TableName.valueOf(Constants.HISTORY_TABLE)); GenerateFlowTestData flowDataGen = new GenerateFlowTestData(); flowDataGen.loadFlow("c1@local", "buser", "AppOne", 1234, "a", numJobsAppOne, baseStats, idService, historyTable); flowDataGen.loadFlow("c2@local", "Muser", "AppTwo", 2345, "b", numJobsAppTwo, baseStats, idService, historyTable); historyTable.close(); JobHistoryService service = new JobHistoryService(UTIL.getConfiguration(), hbaseConnection); List<Flow> actualFlows = service.getFlowTimeSeriesStats("c1@local", "buser", "AppOne", "", 0L, 0L, 1000, null); // serialize flows into json ObjectMapper om = ObjectMapperProvider.createCustomMapper(); om.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); om.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); ByteArrayOutputStream f = new ByteArrayOutputStream(); om.writeValue(f, actualFlows); ByteArrayInputStream is = new ByteArrayInputStream(f.toByteArray()); @SuppressWarnings("unchecked") List<Flow> deserFlows = (List<Flow>) JSONUtil.readJson(is, new TypeReference<List<Flow>>() { }); assertFlowDetails(actualFlows, deserFlows); }
Example #16
Source File: JSONUtil.java From hraven with Apache License 2.0 | 5 votes |
/** * Writes object to the writer as JSON using Jackson and adds a new-line before flushing. * @param writer the writer to write the JSON to * @param object the object to write as JSON * @throws IOException if the object can't be serialized as JSON or written to the writer */ public static void writeJson(Writer writer, Object object) throws IOException { ObjectMapper om = ObjectMapperProvider.createCustomMapper(); om.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); om.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); writer.write(om.writeValueAsString(object)); writer.write("\n"); writer.flush(); }
Example #17
Source File: ObjectMapperProvider.java From at.info-knowledge-base with MIT License | 5 votes |
private static ObjectMapper createDefaultMapper() { final ObjectMapper result = new ObjectMapper(); result.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); result.registerModule(new MrBeanModule()); return result; }
Example #18
Source File: ObjectMapperHelper.java From bintray-client-java with Apache License 2.0 | 5 votes |
private static ObjectMapper buildDetailsMapper() { ObjectMapper mapper = new ObjectMapper(new JsonFactory()); // TODO: when moving to Jackson 2.x these can be replaced with JodaModule mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); mapper.configure(SerializationConfig.Feature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false); //Don't include keys with null values implicitly, only explicitly set values should be sent over mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, false); mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false); mapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); return mapper; }
Example #19
Source File: GenericRecordLogAppender.java From lsmtree with Apache License 2.0 | 5 votes |
/** * @param file root directory for record logs * @param serializer serializer * @param codec compression codec * @param metadataRef if non null, this will contain a reference to metadata after construction if it existed * @param rollFrequency how frequently new record files should be created in milliseconds, but only if there * is a new write. If set to {@link Long#MAX_VALUE}, new record files will only be created when * {@link #flushWriter(java.util.Map)} is called * * @throws IOException if an I/O error occurs */ public GenericRecordLogAppender(@Nonnull File file, @Nonnull Serializer<T> serializer, @Nonnull CompressionCodec codec, @Nullable AtomicReference<Map<String, String>> metadataRef, long rollFrequency) throws IOException { mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); this.file = file; lastPositionPath = new File(this.file, "lastposition.txt"); maxSegmentPath = new File(file, "maxsegment.txt"); metadataPath = new File(file, "metadata.json"); if (metadataPath.exists()) { Map<String, String> metadata = readMetadata(metadataPath, mapper); if (metadataRef != null) metadataRef.set(metadata); lastPosition = Long.parseLong(metadata.get(LAST_POSITION_KEY)); maxSegment = Integer.parseInt(metadata.get(MAX_SEGMENT_KEY)); log.info("lastposition: "+lastPosition); log.info("maxsegment: "+maxSegment); } else { lastPosition = readLongFromFile(lastPositionPath, 0); maxSegment = -1; } writer = maxSegment < 0 ? RecordLogDirectory.Writer.create(file, serializer, codec, rollFrequency) : RecordLogDirectory.Writer.create(file, serializer, codec, rollFrequency, maxSegment); }
Example #20
Source File: DefaultIndexMapper.java From Raigad with Apache License 2.0 | 5 votes |
public DefaultIndexMapper(JsonFactory factory) { super(factory); SimpleModule serializerModule = new SimpleModule("default serializers", new Version(1, 0, 0, null)); registerModule(serializerModule); configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); configure(SerializationConfig.Feature.AUTO_DETECT_GETTERS, false); configure(SerializationConfig.Feature.AUTO_DETECT_FIELDS, false); configure(SerializationConfig.Feature.INDENT_OUTPUT, false); }
Example #21
Source File: DefaultMasterNodeInfoMapper.java From Raigad with Apache License 2.0 | 5 votes |
public DefaultMasterNodeInfoMapper(JsonFactory factory) { super(factory); SimpleModule serializerModule = new SimpleModule("default serializers", new Version(1, 0, 0, null)); registerModule(serializerModule); configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); }
Example #22
Source File: TestHelixAdminScenariosRest.java From helix with Apache License 2.0 | 5 votes |
public static String ObjectToJson(Object object) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); SerializationConfig serializationConfig = mapper.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); StringWriter sw = new StringWriter(); mapper.writeValue(sw, object); return sw.toString(); }
Example #23
Source File: ClusterRepresentationUtil.java From helix with Apache License 2.0 | 5 votes |
public static String ObjectToJson(Object object) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); SerializationConfig serializationConfig = mapper.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); StringWriter sw = new StringWriter(); mapper.writeValue(sw, object); return sw.toString(); }
Example #24
Source File: ObjectMapperResolver.java From localization_nifi with Apache License 2.0 | 5 votes |
public ObjectMapperResolver() throws Exception { mapper = new ObjectMapper(); final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(); final SerializationConfig serializationConfig = mapper.getSerializationConfig(); final DeserializationConfig deserializationConfig = mapper.getDeserializationConfig(); mapper.setSerializationConfig(serializationConfig.withSerializationInclusion(Inclusion.NON_NULL).withAnnotationIntrospector(jaxbIntrospector)); mapper.setDeserializationConfig(deserializationConfig.withAnnotationIntrospector(jaxbIntrospector)); }
Example #25
Source File: ObjectMapperResolver.java From localization_nifi with Apache License 2.0 | 5 votes |
public ObjectMapperResolver() throws Exception { mapper = new ObjectMapper(); final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(); final SerializationConfig serializationConfig = mapper.getSerializationConfig(); final DeserializationConfig deserializationConfig = mapper.getDeserializationConfig(); mapper.setSerializationConfig(serializationConfig.withSerializationInclusion(Inclusion.NON_NULL).withAnnotationIntrospector(jaxbIntrospector)); mapper.setDeserializationConfig(deserializationConfig.withAnnotationIntrospector(jaxbIntrospector)); }
Example #26
Source File: WriteJsonServletAction.java From ureport with Apache License 2.0 | 5 votes |
protected void writeObjectToJson(HttpServletResponse resp,Object obj) throws ServletException, IOException{ resp.setContentType("text/json"); resp.setCharacterEncoding("UTF-8"); ObjectMapper mapper=new ObjectMapper(); mapper.setSerializationInclusion(Inclusion.NON_NULL); mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); OutputStream out = resp.getOutputStream(); try { mapper.writeValue(out, obj); } finally { out.flush(); out.close(); } }
Example #27
Source File: JacksonObjectMapperProvider.java From Bats with Apache License 2.0 | 5 votes |
/** * <p>Constructor for JacksonObjectMapperProvider.</p> */ public JacksonObjectMapperProvider() { this.objectMapper = new ObjectMapper(); objectMapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true); objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); module.addSerializer(ObjectMapperString.class, new RawSerializer<>(Object.class)); module.addSerializer(JSONObject.class, new RawSerializer<>(Object.class)); module.addSerializer(JSONArray.class, new RawSerializer<>(Object.class)); objectMapper.registerModule(module); }
Example #28
Source File: JsonPrettyString.java From jira-rest-client with Apache License 2.0 | 5 votes |
/** * Map to Pretty Json String * * @param map map data * @return Json String */ public static String mapToPrettyJsonString(Map<String, Object> map) { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); String jsonStr = ""; try { jsonStr = mapper.writeValueAsString(map); } catch (IOException e) { e.printStackTrace(); } return jsonStr; }
Example #29
Source File: JsonPrettyString.java From jira-rest-client with Apache License 2.0 | 5 votes |
final public String toPrettyJsonString() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); StringWriter sw = new StringWriter(); try { mapper.writeValue(sw, this); } catch (IOException e) { return toString(); } return sw.toString(); }
Example #30
Source File: JsonGenerator.java From OWL2VOWL with MIT License | 5 votes |
public void export(Exporter exporter) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false); mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT); invoke(root); exporter.write(mapper.writeValueAsString(root)); }