/** * {@inheritDoc} */ @Override public void writeComma() { JsonValueContext valueContext = peekValueContext(); if (valueContext != null) { valueContext.incrementIndex(); } super.writeComma(); }
/** * {@inheritDoc} */ @Override public void pushName(final String name, final boolean withComma) { JsonValueContext valueContext = peekValueContext(); if (valueContext != null) { valueContext.setPropertyName(name); } super.pushName(name, withComma); }
/** * Parses input JSON to {@link JsonObject}, special case of {@link #parse(String, Class)}. */ public JsonObject parseAsJsonObject(final String input) { return new JsonObject(parse(input)); }
@Override public void serializeValue(final JsonContext jsonContext, final Object value) { jsonContext.writeOpenObject(); BeanSerializer beanVisitor = new BeanSerializer(jsonContext, value); beanVisitor.serialize(); jsonContext.writeCloseObject(); }
private JsonObject createJsonObject() { JsonObject obj = new JsonObject(); obj.put("mystr", "bar"); obj.put("myint", Integer.MAX_VALUE); obj.put("mylong", Long.MAX_VALUE); obj.put("myfloat", Float.MAX_VALUE); obj.put("mydouble", Double.MAX_VALUE); obj.put("myboolean", true); obj.put("mybinary", randomByteArray(100)); return obj; }
/** * Static ctor. */ public static JsonSerializer create() { return new JsonSerializer(); }
/** * Serializes object into provided appendable. */ public void serialize(final Object source, final Appendable target) { JsonContext jsonContext = createJsonContext(target); jsonContext.serialize(source); }
/** * Resets JSON parser, so it can be reused. */ protected void reset() { this.ndx = 0; this.textLen = 0; this.path = new Path(); if (useAltPaths) { path.altPath = new Path(); } if (classMetadataName != null) { mapToBean = createMapToBean(classMetadataName); } }
/** * Defines custom {@link jodd.json.TypeJsonSerializer} for given type. */ public JsonSerializer withSerializer(final Class type, final TypeJsonSerializer typeJsonSerializer) { if (typeSerializersMap == null) { typeSerializersMap = new TypeJsonSerializerMap(TypeJsonSerializerMap.get()); } typeSerializersMap.register(type, typeJsonSerializer); return this; }
@Override public boolean accept(final Path value, final PathQuery rule, final boolean include) { return rule.matches(value); } };
/** * Pop last element from the path. */ public CharSequence pop(){ if (altPath != null) { altPath.pop(); } return paths[--index]; }
/** * Consumes char at current position. If char is different, throws the exception. */ protected void consume(final char c) { if (input[ndx] != c) { syntaxError("Invalid char: expected " + c); } ndx++; }
private void ident(final JsonContext jsonContext) { for (int i = 0; i < deep; i++) { for (int j = 0; j < identSize; j++) { jsonContext.write(identChar); } } }
@Override public Object get() { final int currentNdx = jsonParser.ndx; final Path currentPath = jsonParser.path; jsonParser.ndx = ndx; jsonParser.path = path; final Object object = jsonParser.parseObjectContent(targetType, keyType, componentType); jsonParser.ndx = currentNdx; jsonParser.path = currentPath; return object; } }
/** * Writes close array sign. */ public void writeCloseArray() { write(']'); }
/** * Creates new instance of {@link jodd.json.MapToBean}. */ protected MapToBean createMapToBean(final String classMetadataName) { return new MapToBean(this, classMetadataName); }