@Override public void serializeValue(final JsonContext jsonContext, final Map<?, ?> map) { if (map.isEmpty() && jsonContext.isExcludeEmpty()) { return; } jsonContext.writeOpenObject(); int count = 0; Path currentPath = jsonContext.getPath(); for (Map.Entry<?, ?> entry : map.entrySet()) { Object key = entry.getKey(); Object value = entry.getValue(); count = serializeKeyValue(jsonContext, currentPath, key, value, count); } jsonContext.writeCloseObject(); }
@Override public boolean serialize(final JsonContext jsonContext, final Character value) { jsonContext.writeString(value.toString()); return true; } }
/** * Creates new JSON context. */ public JsonContext createJsonContext(final Appendable appendable) { return new JsonContext(this, appendable); } }
/** * Invoked on serializable properties, that have passed all the rules. * Property type is <code>null</code> for metadata class name property. */ protected void onSerializableProperty(final String propertyName, final Class propertyType, final Object value) { jsonContext.pushName(propertyName, count > 0); jsonContext.serialize(value); if (jsonContext.isNamePopped()) { count++; } }
@Override public boolean serialize(final JsonContext jsonContext, final boolean[] array) { if (array.length == 0 && jsonContext.isExcludeEmpty()) { return true; } jsonContext.writeOpenArray(); for (int i = 0; i < array.length; i++) { if (i > 0) { jsonContext.writeComma(); } jsonContext.write(Boolean.toString(array[i])); } jsonContext.writeCloseArray(); return true; }
@Override public boolean serialize(final JsonContext jsonContext, final Float value) { if (value.isNaN()) { jsonContext.writeString("NaN"); return true; } if (value == Float.POSITIVE_INFINITY) { jsonContext.writeString("+Infinity"); return true; } if (value == Float.NEGATIVE_INFINITY) { jsonContext.writeString("-Infinity"); return true; } jsonContext.write(value.toString()); return true; } }
@Override public void serializeValue(final JsonContext jsonContext, final JsonObject jsonObject) { jsonContext.writeOpenObject(); int count = 0; Path currentPath = jsonContext.getPath(); for (Map.Entry<String, ?> entry : jsonObject.map().entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); count = serializeKeyValue(jsonContext, currentPath, key, value, count); } jsonContext.writeCloseObject(); } }
@Override public void serializeValue(final JsonContext jsonContext, final Object value) { jsonContext.writeOpenObject(); BeanSerializer beanVisitor = new BeanSerializer(jsonContext, value); beanVisitor.serialize(); jsonContext.writeCloseObject(); }
@Override public boolean serialize(JsonContext jsonContext, String value) { JsonValueContext jsonValueContext = jsonContext.peekValueContext(); if (jsonValueContext.getIndex() == 1) { value = value.toUpperCase(); } jsonContext.writeString(value); return true; } }
@Override public void serializeValue(final JsonContext jsonContext, final Object array) { final int length = getLength((K[]) array); if (length == 0 && jsonContext.isExcludeEmpty()) { return; } jsonContext.writeOpenArray(); int count = 0; for (int i = 0; i < length; i++) { if (count > 0) { jsonContext.writeComma(); } if (jsonContext.serialize(get((K[]) array, i))) { count++; } } jsonContext.writeCloseArray(); } }
if ((value == null) && jsonContext.isExcludeNulls()) { return count; include = jsonContext.matchIgnoredPropertyTypes(value.getClass(), false, include); include = jsonContext.matchPathToQueries(include); jsonContext.pushName(null, count > 0); } else { jsonContext.pushName(key.toString(), count > 0); jsonContext.serialize(value); if (jsonContext.isNamePopped()) { count++;
@Override public void writeCloseObject() { deep--; if (breakOnNewObject) { breakLine(this); } super.writeCloseObject(); }
/** * {@inheritDoc} */ @Override public void pushName(final String name, final boolean withComma) { JsonValueContext valueContext = peekValueContext(); if (valueContext != null) { valueContext.setPropertyName(name); } super.pushName(name, withComma); }
/** * Reads property value and {@link #onSerializableProperty(String, Class, Object) serializes it}. */ @Override protected final void onSerializableProperty(String propertyName, final PropertyDescriptor propertyDescriptor) { final Object value; if (propertyDescriptor == null) { // metadata - classname value = source.getClass().getName(); } else { value = readProperty(source, propertyDescriptor); if ((value == null) && jsonContext.isExcludeNulls()) { return; } // change name for properties propertyName = typeData.resolveJsonName(propertyName); } onSerializableProperty( propertyName, propertyDescriptor == null ? null : propertyDescriptor.getType(), value); }
@Override public boolean serialize(final JsonContext jsonContext, final float[] array) { if (array.length == 0 && jsonContext.isExcludeEmpty()) { return true; } jsonContext.writeOpenArray(); for (int i = 0; i < array.length; i++) { if (i > 0) { jsonContext.writeComma(); } jsonContext.write(Float.toString(array[i])); } jsonContext.writeCloseArray(); return true; } }
@Override public void serializeValue(JsonContext jsonContext, PodMultiMap<?> map) { jsonContext.writeOpenObject(); int count = 0; Path currentPath = jsonContext.getPath(); for (String key : map.names()) { final List<?> list = map.getAll(key); Object value = list.size() == 1 ? list.get(0) : list; count = serializeKeyValue( jsonContext, currentPath, key, value, count); } jsonContext.writeCloseObject(); }
@Override public void serializeValue(final JsonContext jsonContext, Object value) { jsonContext.writeOpenObject(); BeanSerializer beanVisitor = new BeanSerializer(jsonContext, value) { @Override protected void onSerializableProperty(String propertyName, Class propertyType, Object value) { if (value == null) { return; } super.onSerializableProperty(propertyName, propertyType, value); } }; beanVisitor.serialize(); jsonContext.writeCloseObject(); } });
@Override public boolean serialize(JsonContext jsonContext, String value) { JsonValueContext jsonValueContext = jsonContext.peekValueContext(); String propertyName = jsonValueContext.getPropertyName(); if (propertyName != null && propertyName.equals("name")) { value = value.toUpperCase(); } jsonContext.writeString(value); return true; } }
@Override public boolean serialize(final JsonContext jsonContext, final JsonArray jsonArray) { final int length = jsonArray.size(); if (length == 0 && jsonContext.isExcludeEmpty()) { return true; } jsonContext.writeOpenArray(); int count = 0; for (int i = 0; i < length; i++) { if (count > 0) { jsonContext.writeComma(); } if (jsonContext.serialize(jsonArray.getValue(i))) { count++; } } jsonContext.writeCloseArray(); return true; } }
@Override public boolean serialize(final JsonContext jsonContext, final Double value) { if (value.isNaN()) { jsonContext.writeString("NaN"); return true; } if (value == Double.POSITIVE_INFINITY) { jsonContext.writeString("+Infinity"); return true; } if (value == Double.NEGATIVE_INFINITY) { jsonContext.writeString("-Infinity"); return true; } jsonContext.write(value.toString()); return true; } }