/** * Appends char to the buffer. Used internally. */ protected void write(final char c) { try { out.append(c); } catch (IOException ioex) { throw new JsonException(ioex); } }
private void checkClassName(final List<String> classnameWhitelist, final String className) { if (classnameWhitelist == null) { return; } classnameWhitelist.forEach(pattern -> { if (!Wildcard.equalsOrMatch(className, pattern)) { throw new JsonException("Class can't be loaded as it is not whitelisted: " + className); } }); }
/** * Appends char sequence to the buffer. Used for numbers, nulls, booleans, etc. */ public void write(final CharSequence charSequence) { popName(); try { out.append(charSequence); } catch (IOException ioex) { throw new JsonException(ioex); } }
/** * Creates new type for JSON array objects. * It returns a collection. * Later, the collection will be converted into the target type. */ @SuppressWarnings("unchecked") protected Collection<Object> newArrayInstance(final Class targetType) { if (targetType == null || targetType == List.class || targetType == Collection.class || targetType.isArray()) { return listSupplier.get(); } if (targetType == Set.class) { return new HashSet<>(); } try { return (Collection<Object>) targetType.getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new JsonException(e); } }
/** * Throws {@link jodd.json.JsonException} indicating a syntax error. */ protected void syntaxError(final String message) { String left = "..."; String right = "..."; int offset = 10; int from = ndx - offset; if (from < 0) { from = 0; left = StringPool.EMPTY; } int to = ndx + offset; if (to > input.length) { to = input.length; right = StringPool.EMPTY; } final CharSequence str = CharArraySequence.of(input, from, to - from); throw new JsonException( "Syntax error! " + message + "\n" + "offset: " + ndx + " near: \"" + left + str + right + "\""); }
/** * Converts type of the given value. */ protected Object convertType(final Object value, final Class targetType) { Class valueClass = value.getClass(); if (valueClass == targetType) { return value; } try { return TypeConverterManager.get().convertType(value, targetType); } catch (Exception ex) { throw new JsonException("Type conversion failed", ex); } }
/** * Creates new object or a <code>HashMap</code> if type is not specified. */ protected Object newObjectInstance(final Class targetType) { if (targetType == null || targetType == Map.class) { return mapSupplier.get(); } ClassDescriptor cd = ClassIntrospector.get().lookup(targetType); CtorDescriptor ctorDescriptor = cd.getDefaultCtorDescriptor(true); if (ctorDescriptor == null) { throw new JsonException("Default ctor not found for: " + targetType.getName()); } try { // return ClassUtil.newInstance(targetType); return ctorDescriptor.getConstructor().newInstance(); } catch (Exception e) { throw new JsonException(e); } }
/** * Reads property using property descriptor. */ private Object readProperty(final Object source, final PropertyDescriptor propertyDescriptor) { Getter getter = propertyDescriptor.getGetter(declared); if (getter != null) { try { return getter.invokeGetter(source); } catch (Exception ex) { throw new JsonException(ex); } } return null; }
throw new JsonException("Illegal JSON type: " + value.getClass());
protected Object convert(final Object value, final Class targetType) { Class valueClass = value.getClass(); if (valueClass == targetType) { return value; } if (value instanceof Map) { if (targetType == Map.class) { return value; } return map2bean((Map) value, targetType); } try { return TypeConverterManager.get().convertType(value, targetType); } catch (Exception ex) { throw new JsonException("Type conversion failed", ex); } }
@Override public boolean serialize(final JsonContext jsonContext, final File file) { switch (serializationType) { case PATH: jsonContext.writeString(file.getAbsolutePath()); break; case NAME: jsonContext.writeString(file.getName()); break; case CONTENT: { byte[] bytes; try { bytes = FileUtil.readBytes(file); } catch (IOException e) { throw new JsonException("Unable to read files content", e); } String encoded = Base64.encodeToString(bytes); jsonContext.writeString(encoded); } break; default: throw new JsonException("Invalid type"); } return true; } }
/** * Injects value into the targets property. */ protected void injectValueIntoObject(final Object target, final PropertyDescriptor pd, final Object value) { Object convertedValue = value; if (value != null) { Class targetClass = pd.getType(); convertedValue = convertType(value, targetClass); } try { Setter setter = pd.getSetter(true); if (setter != null) { setter.invokeSetter(target, convertedValue); } } catch (Exception ex) { throw new JsonException(ex); } }
targetType = ClassLoaderUtil.loadClass(className); } catch (ClassNotFoundException cnfex) { throw new JsonException(cnfex);
@Test void testDateTransforming() { JsonParsers.forEachParser(jsonParser -> { final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); Person foo = new Person("Foo", "Bar", new Date(), null, null); try { foo.setBirthdate(df.parse("2009/01/02")); } catch (ParseException pe) { fail(pe); } String json = new JsonSerializer().withSerializer("birthdate", new DateJsonSerializer() { @Override public boolean serialize(JsonContext jsonContext, Date date) { jsonContext.writeString(df.format(date)); return true; } }).serialize(foo); Person newUser = jsonParser .withValueConverter("birthdate", (ValueConverter<String, Date>) data -> { try { return df.parse(data); } catch (ParseException pe) { throw new JsonException(pe); } }) .parse(json, Person.class); assertEquals(foo.getBirthdate(), newUser.getBirthdate()); assertEquals("2009/01/02", df.format(newUser.getBirthdate())); }); }