public JsonMapper(Inclusion inclusion, DateFormat df, boolean replaceNull) { mapper = new ObjectMapper(); // 设置输出时包含属性的风格 // mapper.setSerializationInclusion(inclusion); // // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性 // mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); // // 禁止使用int代表Enum的order()來反序列化Enum // mapper.configure(DeserializationConfig.Feature.FAIL_ON_NUMBERS_FOR_ENUMS, true); // 允许单引号 mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); mapper.setDateFormat(df); if (replaceNull) { // null 转换为 "" mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(""); } }); } }
@JsonSerialize(include = Inclusion.NON_NULL) public UUID getId() { if ( id != null ) { return id; } if ( entity != null ) { id = entity.getUuid(); return id; } if ( ( ids != null ) && ( ids.size() > 0 ) ) { id = ids.get( 0 ); return id; } if ( ( entities != null ) && ( entities.size() > 0 ) ) { entity = entities.get( 0 ); id = entity.getUuid(); return id; } if ( ( refs != null ) && ( refs.size() > 0 ) ) { EntityRef ref = refs.get( 0 ); id = ref.getUuid(); } return id; }
@JsonSerialize(include = Inclusion.NON_NULL) public EntityRef getRef() { if ( ref != null ) { return ref; } ref = getEntity(); if ( ref != null ) { return ref; } UUID u = getId(); if ( u != null ) { String type= null; if(refs!=null && refs.size()>0){ type = refs.get(0).getType(); } return ref( type,u ); } return null; }
/** * 返回日期处理成功结果 * @param data 数据节点对象 * @return JSON结果 */ public static String success(Object data, String format) { Map<String, Object> map = new HashMap<String, Object>(); map.put("code", GlobalResultStatus.SUCCESS.getCode()); if (data != null) { map.put("data", data); } JsonMapper mapper = new JsonMapper(Inclusion.NON_NULL, new SimpleDateFormat(format)); return mapper.toJson(map); }
boolean valueAllowed(Object value, Class cls) { return value != null || (serializerProvider.getConfig() != null && serializerProvider.getConfig().getSerializationInclusion() == Include.ALWAYS && getAnnotation(cls, JsonSerialize.class) == null) || (getAnnotation(cls, JsonSerialize.class) != null && readClassAnnotation(cls, JsonSerialize.class, "include") == Inclusion.ALWAYS); }
/** * 将对象转换成json, 可以设置输出属性 * * * @param src 对象 * @param inclusion 传入一个枚举值, 设置输出属性 * @return 返回json字符串 */ public static <T> String toJson(T src, Inclusion inclusion) { if (src instanceof String) { return (String) src; } else { ObjectMapper customMapper = generateMapper(inclusion); try { return customMapper.writeValueAsString(src); } catch (JsonProcessingException e) { LOG.error("JsonProcessingException: ", e); } } return null; }
/** * 通过Inclusion创建ObjectMapper对象 * * * @param inclusion 传入一个枚举值, 设置输出属性 * @return 返回ObjectMapper对象 */ private static ObjectMapper generateMapper(Inclusion inclusion) { ObjectMapper customMapper = new ObjectMapper(); // 所有日期格式都统一为以下样式 customMapper.setDateFormat(new SimpleDateFormat(DATE_TIME_FORMAT)); return customMapper; }
@JsonSerialize(include = Inclusion.NON_NULL) public Entity getEntity() { mergeEntitiesWithMetadata(); if ( entity != null ) { return entity; } if ( ( entities != null ) && ( entities.size() > 0 ) ) { entity = entities.get( 0 ); return entity; } return null; }
@JsonSerialize(include = Inclusion.NON_NULL) public Map<UUID, Entity> getEntitiesMap() { if ( entitiesMap != null ) { return entitiesMap; } if ( getEntities() != null ) { entitiesMap = new LinkedHashMap<UUID, Entity>(); for ( Entity entity : getEntities() ) { entitiesMap.put( entity.getUuid(), entity ); } } return entitiesMap; }
public JsonMapper() { this(Inclusion.NON_NULL, null, false); }
public JsonMapper(Inclusion inclusion) { this(inclusion, null, false); }
public JsonMapper(Inclusion inclusion, DateFormat df) { this(inclusion, df, false); }
/** * 创建输出全部属性到Json字符串的Mapper. */ public static JsonMapper buildNormalMapper() { return new JsonMapper(Inclusion.ALWAYS); }
/** * 创建只输出非空属性到Json字符串的Mapper. */ public static JsonMapper buildNonNullMapper() { return new JsonMapper(Inclusion.NON_NULL); }
/** * 创建只输出初始值被改变的属性到Json字符串的Mapper. */ public static JsonMapper buildNonDefaultMapper() { return new JsonMapper(Inclusion.NON_DEFAULT); }
/** * 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper. */ public static JsonMapper buildNonEmptyMapper() { return new JsonMapper(Inclusion.NON_EMPTY); }
/** * 报表返回格式日期处理成功结果 * @param data 数据节点对象 * @return JSON结果 */ public static String reportSuccess(Object data, String format) { JsonMapper mapper = new JsonMapper(Inclusion.NON_NULL, new SimpleDateFormat(format)); return mapper.toJson(data); }
/** * Returns the OAuth token that was sent with the request * * @return the OAuth token */ @JsonProperty("access_token") @JsonSerialize(include = Inclusion.NON_NULL) public String getAccessToken() { return accessToken; }
/** * Returns the 'error_description' property of the response. * * @return the error description */ @JsonSerialize(include = Inclusion.NON_NULL) @JsonProperty("error_description") public String getErrorDescription() { return errorDescription; }
/** * Returns the 'error_uri' property of the response. * * @return the error URI */ @JsonSerialize(include = Inclusion.NON_NULL) @JsonProperty("error_uri") public String getErrorUri() { return errorUri; }
@JsonSerialize(include = Inclusion.NON_NULL) public UUID getLast() { return last; }
@JsonSerialize(include = Inclusion.NON_NULL) public UUID getUser() { return user; }
@JsonSerialize(include = Inclusion.NON_NULL) public UUID getGroup() { return group; }
@JsonSerialize(include = Inclusion.NON_NULL) public String getCategory() { return category; }
@JsonSerialize(include = Inclusion.NON_NULL) public String getName() { return name; }
@JsonSerialize(include = Inclusion.NON_NULL) public List<AggregateCounter> getValues() { return values; }
@JsonSerialize(include = Inclusion.NON_NULL) public UUID getQueue() { return queue; }
@JsonSerialize(include = Inclusion.NON_NULL) public Long getTimestamp() { return JsonUtils.getLongProperty(properties, PROPERTY_TIMESTAMP); }
@ApiModelProperty("Retention settings for metrics, expressed in days") @JsonProperty("retentions") @JsonSerialize(include = Inclusion.NON_EMPTY, keyUsing = MetricTypeKeySerializer.class) public Map<MetricType<?>, Integer> getRetentionSettings() { return retentionSettings; }
@ApiModelProperty("Metric tags") @JsonSerialize(include = Inclusion.NON_EMPTY) public Map<String, String> getTags() { return tags; }
@ApiModelProperty("Metric data points") @JsonSerialize(include = Inclusion.NON_EMPTY) public List<DataPoint<T>> getDataPoints() { return dataPoints; }
@ApiModelProperty("Timestamp of the metric's oldest data point") @JsonSerialize(include = Inclusion.NON_EMPTY) public Long getMinTimestamp() { return minTimestamp; }
@ApiModelProperty("Timestamp of the metric's most recent data point") @JsonSerialize(include = Inclusion.NON_EMPTY) public Long getMaxTimestamp() { return maxTimestamp; }
@JsonSerialize(include = Inclusion.NON_NULL) public Boolean getDisabled() { return disabled; }
@JsonSerialize(include = Inclusion.NON_NULL) public String getDisplayName() { return displayName; }
@JsonSerialize(include = Inclusion.NON_NULL) public Set<String> getPermissions() { return permissions; }
@JsonSerialize( include = Inclusion.NON_NULL ) @JsonProperty( "error_description" ) public String getErrorDescription() { return errorDescription; }
@JsonSerialize(include = Inclusion.NON_NULL) public List<UUID> getActivities() { return activities; }