Mercurial Hosting > luan
changeset 804:9f5ba30722bb
remove org.eclipse.jetty.util.ajax
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 07 Sep 2016 21:28:02 -0600 |
parents | 166b16bda630 |
children | fc3e366caa51 |
files | src/org/eclipse/jetty/util/ajax/JSON.java src/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java src/org/eclipse/jetty/util/ajax/JSONDateConvertor.java src/org/eclipse/jetty/util/ajax/JSONEnumConvertor.java src/org/eclipse/jetty/util/ajax/JSONObjectConvertor.java src/org/eclipse/jetty/util/ajax/JSONPojoConvertor.java src/org/eclipse/jetty/util/ajax/JSONPojoConvertorFactory.java |
diffstat | 7 files changed, 0 insertions(+), 2546 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/util/ajax/JSON.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1640 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.io.Externalizable; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.lang.reflect.Array; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.eclipse.jetty.util.IO; -import org.eclipse.jetty.util.Loader; -import org.eclipse.jetty.util.QuotedStringTokenizer; -import org.eclipse.jetty.util.TypeUtil; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; - -/** - * JSON Parser and Generator. - * <p /> - * This class provides some static methods to convert POJOs to and from JSON - * notation. The mapping from JSON to java is: - * - * <pre> - * object ==> Map - * array ==> Object[] - * number ==> Double or Long - * string ==> String - * null ==> null - * bool ==> Boolean - * </pre> - - * The java to JSON mapping is: - * - * <pre> - * String --> string - * Number --> number - * Map --> object - * List --> array - * Array --> array - * null --> null - * Boolean--> boolean - * Object --> string (dubious!) - * </pre> - * - * The interface {@link JSON.Convertible} may be implemented by classes that - * wish to externalize and initialize specific fields to and from JSON objects. - * Only directed acyclic graphs of objects are supported. - * <p /> - * The interface {@link JSON.Generator} may be implemented by classes that know - * how to render themselves as JSON and the {@link #toString(Object)} method - * will use {@link JSON.Generator#addJSON(Appendable)} to generate the JSON. - * The class {@link JSON.Literal} may be used to hold pre-generated JSON object. - * <p /> - * The interface {@link JSON.Convertor} may be implemented to provide static - * converters for objects that may be registered with - * {@link #registerConvertor(Class, Convertor)}. - * These converters are looked up by class, interface and super class by - * {@link #getConvertor(Class)}. - * <p /> - * If a JSON object has a "class" field, then a java class for that name is - * loaded and the method {@link #convertTo(Class,Map)} is used to find a - * {@link JSON.Convertor} for that class. - * <p /> - * If a JSON object has a "x-class" field then a direct lookup for a - * {@link JSON.Convertor} for that class name is done (without loading the class). - */ -public class JSON -{ - static final Logger LOG = Log.getLogger(JSON.class); - public final static JSON DEFAULT = new JSON(); - - private Map<String, Convertor> _convertors = new ConcurrentHashMap<String, Convertor>(); - private int _stringBufferSize = 1024; - - public JSON() - { - } - - /** - * @return the initial stringBuffer size to use when creating JSON strings - * (default 1024) - */ - public int getStringBufferSize() - { - return _stringBufferSize; - } - - /** - * @param stringBufferSize - * the initial stringBuffer size to use when creating JSON - * strings (default 1024) - */ - public void setStringBufferSize(int stringBufferSize) - { - _stringBufferSize = stringBufferSize; - } - - /** - * Register a {@link Convertor} for a class or interface. - * - * @param forClass - * The class or interface that the convertor applies to - * @param convertor - * the convertor - */ - public static void registerConvertor(Class forClass, Convertor convertor) - { - DEFAULT.addConvertor(forClass,convertor); - } - - public static JSON getDefault() - { - return DEFAULT; - } - - @Deprecated - public static void setDefault(JSON json) - { - } - - public static String toString(Object object) - { - StringBuilder buffer = new StringBuilder(DEFAULT.getStringBufferSize()); - DEFAULT.append(buffer,object); - return buffer.toString(); - } - - public static String toString(Map object) - { - StringBuilder buffer = new StringBuilder(DEFAULT.getStringBufferSize()); - DEFAULT.appendMap(buffer,object); - return buffer.toString(); - } - - public static String toString(Object[] array) - { - StringBuilder buffer = new StringBuilder(DEFAULT.getStringBufferSize()); - DEFAULT.appendArray(buffer,array); - return buffer.toString(); - } - - /** - * @param s - * String containing JSON object or array. - * @return A Map, Object array or primitive array parsed from the JSON. - */ - public static Object parse(String s) - { - return DEFAULT.parse(new StringSource(s),false); - } - - /** - * @param s - * String containing JSON object or array. - * @param stripOuterComment - * If true, an outer comment around the JSON is ignored. - * @return A Map, Object array or primitive array parsed from the JSON. - */ - public static Object parse(String s, boolean stripOuterComment) - { - return DEFAULT.parse(new StringSource(s),stripOuterComment); - } - - /** - * @param in - * Reader containing JSON object or array. - * @return A Map, Object array or primitive array parsed from the JSON. - */ - public static Object parse(Reader in) throws IOException - { - return DEFAULT.parse(new ReaderSource(in),false); - } - - /** - * @param in - * Reader containing JSON object or array. - * @param stripOuterComment - * If true, an outer comment around the JSON is ignored. - * @return A Map, Object array or primitive array parsed from the JSON. - */ - public static Object parse(Reader in, boolean stripOuterComment) throws IOException - { - return DEFAULT.parse(new ReaderSource(in),stripOuterComment); - } - - /** - * @deprecated use {@link #parse(Reader)} - * @param in - * Reader containing JSON object or array. - * @return A Map, Object array or primitive array parsed from the JSON. - */ - @Deprecated - public static Object parse(InputStream in) throws IOException - { - return DEFAULT.parse(new StringSource(IO.toString(in)),false); - } - - /** - * @deprecated use {@link #parse(Reader, boolean)} - * @param in - * Stream containing JSON object or array. - * @param stripOuterComment - * If true, an outer comment around the JSON is ignored. - * @return A Map, Object array or primitive array parsed from the JSON. - */ - @Deprecated - public static Object parse(InputStream in, boolean stripOuterComment) throws IOException - { - return DEFAULT.parse(new StringSource(IO.toString(in)),stripOuterComment); - } - - /** - * Convert Object to JSON - * - * @param object - * The object to convert - * @return The JSON String - */ - public String toJSON(Object object) - { - StringBuilder buffer = new StringBuilder(getStringBufferSize()); - append(buffer,object); - return buffer.toString(); - } - - /** - * Convert JSON to Object - * - * @param json - * The json to convert - * @return The object - */ - public Object fromJSON(String json) - { - Source source = new StringSource(json); - return parse(source); - } - - @Deprecated - public void append(StringBuffer buffer, Object object) - { - append((Appendable)buffer,object); - } - - /** - * Append object as JSON to string buffer. - * - * @param buffer - * the buffer to append to - * @param object - * the object to append - */ - public void append(Appendable buffer, Object object) - { - try - { - if (object == null) - { - buffer.append("null"); - } - // Most likely first - else if (object instanceof Map) - { - appendMap(buffer,(Map)object); - } - else if (object instanceof String) - { - appendString(buffer,(String)object); - } - else if (object instanceof Number) - { - appendNumber(buffer,(Number)object); - } - else if (object instanceof Boolean) - { - appendBoolean(buffer,(Boolean)object); - } - else if (object.getClass().isArray()) - { - appendArray(buffer,object); - } - else if (object instanceof Character) - { - appendString(buffer,object.toString()); - } - else if (object instanceof Convertible) - { - appendJSON(buffer,(Convertible)object); - } - else if (object instanceof Generator) - { - appendJSON(buffer,(Generator)object); - } - else - { - // Check Convertor before Collection to support JSONCollectionConvertor - Convertor convertor = getConvertor(object.getClass()); - if (convertor != null) - { - appendJSON(buffer,convertor,object); - } - else if (object instanceof Collection) - { - appendArray(buffer,(Collection)object); - } - else - { - appendString(buffer,object.toString()); - } - } - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendNull(StringBuffer buffer) - { - appendNull((Appendable)buffer); - } - - public void appendNull(Appendable buffer) - { - try - { - buffer.append("null"); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendJSON(final StringBuffer buffer, final Convertor convertor, final Object object) - { - appendJSON((Appendable)buffer,convertor,object); - } - - public void appendJSON(final Appendable buffer, final Convertor convertor, final Object object) - { - appendJSON(buffer,new Convertible() - { - public void fromJSON(Map object) - { - } - - public void toJSON(Output out) - { - convertor.toJSON(object,out); - } - }); - } - - @Deprecated - public void appendJSON(final StringBuffer buffer, Convertible converter) - { - appendJSON((Appendable)buffer,converter); - } - - public void appendJSON(final Appendable buffer, Convertible converter) - { - ConvertableOutput out=new ConvertableOutput(buffer); - converter.toJSON(out); - out.complete(); - } - - @Deprecated - public void appendJSON(StringBuffer buffer, Generator generator) - { - generator.addJSON(buffer); - } - - public void appendJSON(Appendable buffer, Generator generator) - { - generator.addJSON(buffer); - } - - @Deprecated - public void appendMap(StringBuffer buffer, Map<?,?> map) - { - appendMap((Appendable)buffer,map); - } - - public void appendMap(Appendable buffer, Map<?,?> map) - { - try - { - if (map == null) - { - appendNull(buffer); - return; - } - - buffer.append('{'); - Iterator<?> iter = map.entrySet().iterator(); - while (iter.hasNext()) - { - Map.Entry<?,?> entry = (Map.Entry<?,?>)iter.next(); - QuotedStringTokenizer.quote(buffer,entry.getKey().toString()); - buffer.append(':'); - append(buffer,entry.getValue()); - if (iter.hasNext()) - buffer.append(','); - } - - buffer.append('}'); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendArray(StringBuffer buffer, Collection collection) - { - appendArray((Appendable)buffer,collection); - } - - public void appendArray(Appendable buffer, Collection collection) - { - try - { - if (collection == null) - { - appendNull(buffer); - return; - } - - buffer.append('['); - Iterator iter = collection.iterator(); - boolean first = true; - while (iter.hasNext()) - { - if (!first) - buffer.append(','); - - first = false; - append(buffer,iter.next()); - } - - buffer.append(']'); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendArray(StringBuffer buffer, Object array) - { - appendArray((Appendable)buffer,array); - } - - public void appendArray(Appendable buffer, Object array) - { - try - { - if (array == null) - { - appendNull(buffer); - return; - } - - buffer.append('['); - int length = Array.getLength(array); - - for (int i = 0; i < length; i++) - { - if (i != 0) - buffer.append(','); - append(buffer,Array.get(array,i)); - } - - buffer.append(']'); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendBoolean(StringBuffer buffer, Boolean b) - { - appendBoolean((Appendable)buffer,b); - } - - public void appendBoolean(Appendable buffer, Boolean b) - { - try - { - if (b == null) - { - appendNull(buffer); - return; - } - buffer.append(b?"true":"false"); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendNumber(StringBuffer buffer, Number number) - { - appendNumber((Appendable)buffer,number); - } - - public void appendNumber(Appendable buffer, Number number) - { - try - { - if (number == null) - { - appendNull(buffer); - return; - } - buffer.append(String.valueOf(number)); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Deprecated - public void appendString(StringBuffer buffer, String string) - { - appendString((Appendable)buffer,string); - } - - public void appendString(Appendable buffer, String string) - { - if (string == null) - { - appendNull(buffer); - return; - } - - QuotedStringTokenizer.quote(buffer,string); - } - - // Parsing utilities - - protected String toString(char[] buffer, int offset, int length) - { - return new String(buffer,offset,length); - } - - protected Map<String, Object> newMap() - { - return new HashMap<String, Object>(); - } - - protected Object[] newArray(int size) - { - return new Object[size]; - } - - protected JSON contextForArray() - { - return this; - } - - protected JSON contextFor(String field) - { - return this; - } - - protected Object convertTo(Class type, Map map) - { - if (type != null && Convertible.class.isAssignableFrom(type)) - { - try - { - Convertible conv = (Convertible)type.newInstance(); - conv.fromJSON(map); - return conv; - } - catch (Exception e) - { - throw new RuntimeException(e); - } - } - - Convertor convertor = getConvertor(type); - if (convertor != null) - { - return convertor.fromJSON(map); - } - return map; - } - - /** - * Register a {@link Convertor} for a class or interface. - * - * @param forClass - * The class or interface that the convertor applies to - * @param convertor - * the convertor - */ - public void addConvertor(Class forClass, Convertor convertor) - { - _convertors.put(forClass.getName(),convertor); - } - - /** - * Lookup a convertor for a class. - * <p> - * If no match is found for the class, then the interfaces for the class are - * tried. If still no match is found, then the super class and it's - * interfaces are tried recursively. - * - * @param forClass - * The class - * @return a {@link JSON.Convertor} or null if none were found. - */ - protected Convertor getConvertor(Class forClass) - { - Class cls = forClass; - Convertor convertor = _convertors.get(cls.getName()); - if (convertor == null && this != DEFAULT) - convertor = DEFAULT.getConvertor(cls); - - while (convertor == null && cls != Object.class) - { - Class[] ifs = cls.getInterfaces(); - int i = 0; - while (convertor == null && ifs != null && i < ifs.length) - convertor = _convertors.get(ifs[i++].getName()); - if (convertor == null) - { - cls = cls.getSuperclass(); - convertor = _convertors.get(cls.getName()); - } - } - return convertor; - } - - /** - * Register a {@link JSON.Convertor} for a named class or interface. - * - * @param name - * name of a class or an interface that the convertor applies to - * @param convertor - * the convertor - */ - public void addConvertorFor(String name, Convertor convertor) - { - _convertors.put(name,convertor); - } - - /** - * Lookup a convertor for a named class. - * - * @param name - * name of the class - * @return a {@link JSON.Convertor} or null if none were found. - */ - public Convertor getConvertorFor(String name) - { - Convertor convertor = _convertors.get(name); - if (convertor == null && this != DEFAULT) - convertor = DEFAULT.getConvertorFor(name); - return convertor; - } - - public Object parse(Source source, boolean stripOuterComment) - { - int comment_state = 0; // 0=no comment, 1="/", 2="/*", 3="/* *" -1="//" - if (!stripOuterComment) - return parse(source); - - int strip_state = 1; // 0=no strip, 1=wait for /*, 2= wait for */ - - Object o = null; - while (source.hasNext()) - { - char c = source.peek(); - - // handle // or /* comment - if (comment_state == 1) - { - switch (c) - { - case '/': - comment_state = -1; - break; - case '*': - comment_state = 2; - if (strip_state == 1) - { - comment_state = 0; - strip_state = 2; - } - } - } - // handle /* */ comment - else if (comment_state > 1) - { - switch (c) - { - case '*': - comment_state = 3; - break; - case '/': - if (comment_state == 3) - { - comment_state = 0; - if (strip_state == 2) - return o; - } - else - comment_state = 2; - break; - default: - comment_state = 2; - } - } - // handle // comment - else if (comment_state < 0) - { - switch (c) - { - case '\r': - case '\n': - comment_state = 0; - default: - break; - } - } - // handle unknown - else - { - if (!Character.isWhitespace(c)) - { - if (c == '/') - comment_state = 1; - else if (c == '*') - comment_state = 3; - else if (o == null) - { - o = parse(source); - continue; - } - } - } - - source.next(); - } - - return o; - } - - public Object parse(Source source) - { - int comment_state = 0; // 0=no comment, 1="/", 2="/*", 3="/* *" -1="//" - - while (source.hasNext()) - { - char c = source.peek(); - - // handle // or /* comment - if (comment_state == 1) - { - switch (c) - { - case '/': - comment_state = -1; - break; - case '*': - comment_state = 2; - } - } - // handle /* */ comment - else if (comment_state > 1) - { - switch (c) - { - case '*': - comment_state = 3; - break; - case '/': - if (comment_state == 3) - comment_state = 0; - else - comment_state = 2; - break; - default: - comment_state = 2; - } - } - // handle // comment - else if (comment_state < 0) - { - switch (c) - { - case '\r': - case '\n': - comment_state = 0; - break; - default: - break; - } - } - // handle unknown - else - { - switch (c) - { - case '{': - return parseObject(source); - case '[': - return parseArray(source); - case '"': - return parseString(source); - case '-': - return parseNumber(source); - - case 'n': - complete("null",source); - return null; - case 't': - complete("true",source); - return Boolean.TRUE; - case 'f': - complete("false",source); - return Boolean.FALSE; - case 'u': - complete("undefined",source); - return null; - case 'N': - complete("NaN",source); - return null; - - case '/': - comment_state = 1; - break; - - default: - if (Character.isDigit(c)) - return parseNumber(source); - else if (Character.isWhitespace(c)) - break; - return handleUnknown(source,c); - } - } - source.next(); - } - - return null; - } - - protected Object handleUnknown(Source source, char c) - { - throw new IllegalStateException("unknown char '" + c + "'(" + (int)c + ") in " + source); - } - - protected Object parseObject(Source source) - { - if (source.next() != '{') - throw new IllegalStateException(); - Map<String, Object> map = newMap(); - - char next = seekTo("\"}",source); - - while (source.hasNext()) - { - if (next == '}') - { - source.next(); - break; - } - - String name = parseString(source); - seekTo(':',source); - source.next(); - - Object value = contextFor(name).parse(source); - map.put(name,value); - - seekTo(",}",source); - next = source.next(); - if (next == '}') - break; - else - next = seekTo("\"}",source); - } - - String xclassname = (String)map.get("x-class"); - if (xclassname != null) - { - Convertor c = getConvertorFor(xclassname); - if (c != null) - return c.fromJSON(map); - LOG.warn("No Convertor for x-class '{}'", xclassname); - } - - String classname = (String)map.get("class"); - if (classname != null) - { - try - { - Class c = Loader.loadClass(JSON.class,classname); - return convertTo(c,map); - } - catch (ClassNotFoundException e) - { - LOG.warn("No Class for '{}'", classname); - } - } - - return map; - } - - protected Object parseArray(Source source) - { - if (source.next() != '[') - throw new IllegalStateException(); - - int size = 0; - ArrayList list = null; - Object item = null; - boolean coma = true; - - while (source.hasNext()) - { - char c = source.peek(); - switch (c) - { - case ']': - source.next(); - switch (size) - { - case 0: - return newArray(0); - case 1: - Object array = newArray(1); - Array.set(array,0,item); - return array; - default: - return list.toArray(newArray(list.size())); - } - - case ',': - if (coma) - throw new IllegalStateException(); - coma = true; - source.next(); - break; - - default: - if (Character.isWhitespace(c)) - source.next(); - else - { - coma = false; - if (size++ == 0) - item = contextForArray().parse(source); - else if (list == null) - { - list = new ArrayList(); - list.add(item); - item = contextForArray().parse(source); - list.add(item); - item = null; - } - else - { - item = contextForArray().parse(source); - list.add(item); - item = null; - } - } - } - - } - - throw new IllegalStateException("unexpected end of array"); - } - - protected String parseString(Source source) - { - if (source.next() != '"') - throw new IllegalStateException(); - - boolean escape = false; - - StringBuilder b = null; - final char[] scratch = source.scratchBuffer(); - - if (scratch != null) - { - int i = 0; - while (source.hasNext()) - { - if (i >= scratch.length) - { - // we have filled the scratch buffer, so we must - // use the StringBuffer for a large string - b = new StringBuilder(scratch.length * 2); - b.append(scratch,0,i); - break; - } - - char c = source.next(); - - if (escape) - { - escape = false; - switch (c) - { - case '"': - scratch[i++] = '"'; - break; - case '\\': - scratch[i++] = '\\'; - break; - case '/': - scratch[i++] = '/'; - break; - case 'b': - scratch[i++] = '\b'; - break; - case 'f': - scratch[i++] = '\f'; - break; - case 'n': - scratch[i++] = '\n'; - break; - case 'r': - scratch[i++] = '\r'; - break; - case 't': - scratch[i++] = '\t'; - break; - case 'u': - char uc = (char)((TypeUtil.convertHexDigit((byte)source.next()) << 12) + (TypeUtil.convertHexDigit((byte)source.next()) << 8) - + (TypeUtil.convertHexDigit((byte)source.next()) << 4) + (TypeUtil.convertHexDigit((byte)source.next()))); - scratch[i++] = uc; - break; - default: - scratch[i++] = c; - } - } - else if (c == '\\') - { - escape = true; - } - else if (c == '\"') - { - // Return string that fits within scratch buffer - return toString(scratch,0,i); - } - else - { - scratch[i++] = c; - } - } - - // Missing end quote, but return string anyway ? - if (b == null) - return toString(scratch,0,i); - } - else - b = new StringBuilder(getStringBufferSize()); - - // parse large string into string buffer - final StringBuilder builder=b; - while (source.hasNext()) - { - char c = source.next(); - - if (escape) - { - escape = false; - switch (c) - { - case '"': - builder.append('"'); - break; - case '\\': - builder.append('\\'); - break; - case '/': - builder.append('/'); - break; - case 'b': - builder.append('\b'); - break; - case 'f': - builder.append('\f'); - break; - case 'n': - builder.append('\n'); - break; - case 'r': - builder.append('\r'); - break; - case 't': - builder.append('\t'); - break; - case 'u': - char uc = (char)((TypeUtil.convertHexDigit((byte)source.next()) << 12) + (TypeUtil.convertHexDigit((byte)source.next()) << 8) - + (TypeUtil.convertHexDigit((byte)source.next()) << 4) + (TypeUtil.convertHexDigit((byte)source.next()))); - builder.append(uc); - break; - default: - builder.append(c); - } - } - else if (c == '\\') - { - escape = true; - } - else if (c == '\"') - { - break; - } - else - { - builder.append(c); - } - } - return builder.toString(); - } - - public Number parseNumber(Source source) - { - boolean minus = false; - long number = 0; - StringBuilder buffer = null; - - longLoop: while (source.hasNext()) - { - char c = source.peek(); - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - number = number * 10 + (c - '0'); - source.next(); - break; - - case '-': - case '+': - if (number != 0) - throw new IllegalStateException("bad number"); - minus = true; - source.next(); - break; - - case '.': - case 'e': - case 'E': - buffer = new StringBuilder(16); - if (minus) - buffer.append('-'); - buffer.append(number); - buffer.append(c); - source.next(); - break longLoop; - - default: - break longLoop; - } - } - - if (buffer == null) - return minus ? -1 * number : number; - - doubleLoop: while (source.hasNext()) - { - char c = source.peek(); - switch (c) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '-': - case '.': - case '+': - case 'e': - case 'E': - buffer.append(c); - source.next(); - break; - - default: - break doubleLoop; - } - } - return new Double(buffer.toString()); - - } - - protected void seekTo(char seek, Source source) - { - while (source.hasNext()) - { - char c = source.peek(); - if (c == seek) - return; - - if (!Character.isWhitespace(c)) - throw new IllegalStateException("Unexpected '" + c + " while seeking '" + seek + "'"); - source.next(); - } - - throw new IllegalStateException("Expected '" + seek + "'"); - } - - protected char seekTo(String seek, Source source) - { - while (source.hasNext()) - { - char c = source.peek(); - if (seek.indexOf(c) >= 0) - { - return c; - } - - if (!Character.isWhitespace(c)) - throw new IllegalStateException("Unexpected '" + c + "' while seeking one of '" + seek + "'"); - source.next(); - } - - throw new IllegalStateException("Expected one of '" + seek + "'"); - } - - protected static void complete(String seek, Source source) - { - int i = 0; - while (source.hasNext() && i < seek.length()) - { - char c = source.next(); - if (c != seek.charAt(i++)) - throw new IllegalStateException("Unexpected '" + c + " while seeking \"" + seek + "\""); - } - - if (i < seek.length()) - throw new IllegalStateException("Expected \"" + seek + "\""); - } - - private final class ConvertableOutput implements Output - { - private final Appendable _buffer; - char c = '{'; - - private ConvertableOutput(Appendable buffer) - { - _buffer = buffer; - } - - public void complete() - { - try - { - if (c == '{') - _buffer.append("{}"); - else if (c != 0) - _buffer.append("}"); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - public void add(Object obj) - { - if (c == 0) - throw new IllegalStateException(); - append(_buffer,obj); - c = 0; - } - - public void addClass(Class type) - { - try - { - if (c == 0) - throw new IllegalStateException(); - _buffer.append(c); - _buffer.append("\"class\":"); - append(_buffer,type.getName()); - c = ','; - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - public void add(String name, Object value) - { - try - { - if (c == 0) - throw new IllegalStateException(); - _buffer.append(c); - QuotedStringTokenizer.quote(_buffer,name); - _buffer.append(':'); - append(_buffer,value); - c = ','; - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - public void add(String name, double value) - { - try - { - if (c == 0) - throw new IllegalStateException(); - _buffer.append(c); - QuotedStringTokenizer.quote(_buffer,name); - _buffer.append(':'); - appendNumber(_buffer, value); - c = ','; - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - public void add(String name, long value) - { - try - { - if (c == 0) - throw new IllegalStateException(); - _buffer.append(c); - QuotedStringTokenizer.quote(_buffer,name); - _buffer.append(':'); - appendNumber(_buffer, value); - c = ','; - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - public void add(String name, boolean value) - { - try - { - if (c == 0) - throw new IllegalStateException(); - _buffer.append(c); - QuotedStringTokenizer.quote(_buffer,name); - _buffer.append(':'); - appendBoolean(_buffer,value?Boolean.TRUE:Boolean.FALSE); - c = ','; - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - } - - public interface Source - { - boolean hasNext(); - - char next(); - - char peek(); - - char[] scratchBuffer(); - } - - public static class StringSource implements Source - { - private final String string; - private int index; - private char[] scratch; - - public StringSource(String s) - { - string = s; - } - - public boolean hasNext() - { - if (index < string.length()) - return true; - scratch = null; - return false; - } - - public char next() - { - return string.charAt(index++); - } - - public char peek() - { - return string.charAt(index); - } - - @Override - public String toString() - { - return string.substring(0,index) + "|||" + string.substring(index); - } - - public char[] scratchBuffer() - { - if (scratch == null) - scratch = new char[string.length()]; - return scratch; - } - } - - public static class ReaderSource implements Source - { - private Reader _reader; - private int _next = -1; - private char[] scratch; - - public ReaderSource(Reader r) - { - _reader = r; - } - - public void setReader(Reader reader) - { - _reader = reader; - _next = -1; - } - - public boolean hasNext() - { - getNext(); - if (_next < 0) - { - scratch = null; - return false; - } - return true; - } - - public char next() - { - getNext(); - char c = (char)_next; - _next = -1; - return c; - } - - public char peek() - { - getNext(); - return (char)_next; - } - - private void getNext() - { - if (_next < 0) - { - try - { - _next = _reader.read(); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - } - - public char[] scratchBuffer() - { - if (scratch == null) - scratch = new char[1024]; - return scratch; - } - - } - - /** - * JSON Output class for use by {@link Convertible}. - */ - public interface Output - { - public void addClass(Class c); - - public void add(Object obj); - - public void add(String name, Object value); - - public void add(String name, double value); - - public void add(String name, long value); - - public void add(String name, boolean value); - } - - /* ------------------------------------------------------------ */ - /** - * JSON Convertible object. Object can implement this interface in a similar - * way to the {@link Externalizable} interface is used to allow classes to - * provide their own serialization mechanism. - * <p> - * A JSON.Convertible object may be written to a JSONObject or initialized - * from a Map of field names to values. - * <p> - * If the JSON is to be convertible back to an Object, then the method - * {@link Output#addClass(Class)} must be called from within toJSON() - * - */ - public interface Convertible - { - public void toJSON(Output out); - - public void fromJSON(Map object); - } - - /** - * Static JSON Convertor. - * <p> - * may be implemented to provide static convertors for objects that may be - * registered with - * {@link JSON#registerConvertor(Class, org.eclipse.jetty.util.ajax.JSON.Convertor)} - * . These convertors are looked up by class, interface and super class by - * {@link JSON#getConvertor(Class)}. Convertors should be used when the - * classes to be converted cannot implement {@link Convertible} or - * {@link Generator}. - */ - public interface Convertor - { - public void toJSON(Object obj, Output out); - - public Object fromJSON(Map object); - } - - /** - * JSON Generator. A class that can add it's JSON representation directly to - * a StringBuffer. This is useful for object instances that are frequently - * converted and wish to avoid multiple Conversions - */ - public interface Generator - { - public void addJSON(Appendable buffer); - } - - /** - * A Literal JSON generator A utility instance of {@link JSON.Generator} - * that holds a pre-generated string on JSON text. - */ - public static class Literal implements Generator - { - private String _json; - - /** - * Construct a literal JSON instance for use by - * {@link JSON#toString(Object)}. If {@link Log#isDebugEnabled()} is - * true, the JSON will be parsed to check validity - * - * @param json - * A literal JSON string. - */ - public Literal(String json) - { - if (LOG.isDebugEnabled()) // TODO: Make this a configurable option on JSON instead! - parse(json); - _json = json; - } - - @Override - public String toString() - { - return _json; - } - - public void addJSON(Appendable buffer) - { - try - { - buffer.append(_json); - } - catch(IOException e) - { - throw new RuntimeException(e); - } - } - } -}
--- a/src/org/eclipse/jetty/util/ajax/JSONCollectionConvertor.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; - -import org.eclipse.jetty.util.Loader; - -public class JSONCollectionConvertor implements JSON.Convertor -{ - public void toJSON(Object obj, JSON.Output out) - { - out.addClass(obj.getClass()); - out.add("list", ((Collection)obj).toArray()); - } - - public Object fromJSON(Map object) - { - try - { - Collection result = (Collection)Loader.loadClass(getClass(), (String)object.get("class")).newInstance(); - Collections.addAll(result, (Object[])object.get("list")); - return result; - } - catch (Exception x) - { - if (x instanceof RuntimeException) - throw (RuntimeException)x; - throw new RuntimeException(x); - } - } -}
--- a/src/org/eclipse/jetty/util/ajax/JSONDateConvertor.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,107 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.text.DateFormatSymbols; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import java.util.Map; -import java.util.TimeZone; - -import org.eclipse.jetty.util.DateCache; -import org.eclipse.jetty.util.ajax.JSON.Output; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; - -/* ------------------------------------------------------------ */ -/** -* Convert a {@link Date} to JSON. -* If fromJSON is true in the constructor, the JSON generated will -* be of the form {class="java.util.Date",value="1/1/1970 12:00 GMT"} -* If fromJSON is false, then only the string value of the date is generated. -*/ -public class JSONDateConvertor implements JSON.Convertor -{ - private static final Logger LOG = Log.getLogger(JSONDateConvertor.class); - - private final boolean _fromJSON; - private final DateCache _dateCache; - private final SimpleDateFormat _format; - - public JSONDateConvertor() - { - this(false); - } - - public JSONDateConvertor(boolean fromJSON) - { - this(DateCache.DEFAULT_FORMAT,TimeZone.getTimeZone("GMT"),fromJSON); - } - - public JSONDateConvertor(String format,TimeZone zone,boolean fromJSON) - { - _dateCache=new DateCache(format); - _dateCache.setTimeZone(zone); - _fromJSON=fromJSON; - _format=new SimpleDateFormat(format); - _format.setTimeZone(zone); - } - - public JSONDateConvertor(String format, TimeZone zone, boolean fromJSON, Locale locale) - { - _dateCache = new DateCache(format, locale); - _dateCache.setTimeZone(zone); - _fromJSON = fromJSON; - _format = new SimpleDateFormat(format, new DateFormatSymbols(locale)); - _format.setTimeZone(zone); - } - - public Object fromJSON(Map map) - { - if (!_fromJSON) - throw new UnsupportedOperationException(); - try - { - synchronized(_format) - { - return _format.parseObject((String)map.get("value")); - } - } - catch(Exception e) - { - LOG.warn(e); - } - return null; - } - - public void toJSON(Object obj, Output out) - { - String date = _dateCache.format((Date)obj); - if (_fromJSON) - { - out.addClass(obj.getClass()); - out.add("value",date); - } - else - { - out.add(date); - } - } -}
--- a/src/org/eclipse/jetty/util/ajax/JSONEnumConvertor.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.lang.reflect.Method; -import java.util.Map; - -import org.eclipse.jetty.util.Loader; -import org.eclipse.jetty.util.ajax.JSON.Output; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; - -/* ------------------------------------------------------------ */ -/** - * Convert an {@link Enum} to JSON. - * If fromJSON is true in the constructor, the JSON generated will - * be of the form {class="com.acme.TrafficLight",value="Green"} - * If fromJSON is false, then only the string value of the enum is generated. - * - * - */ -public class JSONEnumConvertor implements JSON.Convertor -{ - private static final Logger LOG = Log.getLogger(JSONEnumConvertor.class); - private boolean _fromJSON; - private Method _valueOf; - { - try - { - Class<?> e = Loader.loadClass(getClass(),"java.lang.Enum"); - _valueOf=e.getMethod("valueOf",Class.class,String.class); - } - catch(Exception e) - { - throw new RuntimeException("!Enums",e); - } - } - - public JSONEnumConvertor() - { - this(false); - } - - public JSONEnumConvertor(boolean fromJSON) - { - _fromJSON=fromJSON; - } - - public Object fromJSON(Map map) - { - if (!_fromJSON) - throw new UnsupportedOperationException(); - try - { - Class c=Loader.loadClass(getClass(),(String)map.get("class")); - return _valueOf.invoke(null,c,map.get("value")); - } - catch(Exception e) - { - LOG.warn(e); - } - return null; - } - - public void toJSON(Object obj, Output out) - { - if (_fromJSON) - { - out.addClass(obj.getClass()); - out.add("value",((Enum)obj).name()); - } - else - { - out.add(((Enum)obj).name()); - } - } -}
--- a/src/org/eclipse/jetty/util/ajax/JSONObjectConvertor.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import org.eclipse.jetty.util.ajax.JSON.Output; - -/* ------------------------------------------------------------ */ -/** - * Convert an Object to JSON using reflection on getters methods. - * - * - * - */ -public class JSONObjectConvertor implements JSON.Convertor -{ - private boolean _fromJSON; - private Set _excluded=null; - - public JSONObjectConvertor() - { - _fromJSON=false; - } - - public JSONObjectConvertor(boolean fromJSON) - { - _fromJSON=fromJSON; - } - - /* ------------------------------------------------------------ */ - /** - * @param fromJSON - * @param excluded An array of field names to exclude from the conversion - */ - public JSONObjectConvertor(boolean fromJSON,String[] excluded) - { - _fromJSON=fromJSON; - if (excluded!=null) - _excluded=new HashSet(Arrays.asList(excluded)); - } - - public Object fromJSON(Map map) - { - if (_fromJSON) - throw new UnsupportedOperationException(); - return map; - } - - public void toJSON(Object obj, Output out) - { - try - { - Class c=obj.getClass(); - - if (_fromJSON) - out.addClass(obj.getClass()); - - Method[] methods = obj.getClass().getMethods(); - - for (int i=0;i<methods.length;i++) - { - Method m=methods[i]; - if (!Modifier.isStatic(m.getModifiers()) && - m.getParameterTypes().length==0 && - m.getReturnType()!=null && - m.getDeclaringClass()!=Object.class) - { - String name=m.getName(); - if (name.startsWith("is")) - name=name.substring(2,3).toLowerCase(Locale.ENGLISH)+name.substring(3); - else if (name.startsWith("get")) - name=name.substring(3,4).toLowerCase(Locale.ENGLISH)+name.substring(4); - else - continue; - - if (includeField(name,obj,m)) - out.add(name, m.invoke(obj,(Object[])null)); - } - } - } - catch (Throwable e) - { - throw new IllegalArgumentException(e); - } - } - - protected boolean includeField(String name, Object o, Method m) - { - return _excluded==null || !_excluded.contains(name); - } - -}
--- a/src/org/eclipse/jetty/util/ajax/JSONPojoConvertor.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,431 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.lang.reflect.Array; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -import org.eclipse.jetty.util.ajax.JSON.Output; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.log.Logger; -/** - * Converts POJOs to JSON and vice versa. - * The key difference: - * - returns the actual object from Convertor.fromJSON (JSONObjectConverter returns a Map) - * - the getters/setters are resolved at initialization (JSONObjectConverter resolves it at runtime) - * - correctly sets the number fields - * - */ -public class JSONPojoConvertor implements JSON.Convertor -{ - private static final Logger LOG = Log.getLogger(JSONPojoConvertor.class); - public static final Object[] GETTER_ARG = new Object[]{}, NULL_ARG = new Object[]{null}; - private static final Map<Class<?>, NumberType> __numberTypes = new HashMap<Class<?>, NumberType>(); - - public static NumberType getNumberType(Class<?> clazz) - { - return __numberTypes.get(clazz); - } - - protected boolean _fromJSON; - protected Class<?> _pojoClass; - protected Map<String,Method> _getters = new HashMap<String,Method>(); - protected Map<String,Setter> _setters = new HashMap<String,Setter>(); - protected Set<String> _excluded; - - /** - * @param pojoClass The class to convert - */ - public JSONPojoConvertor(Class<?> pojoClass) - { - this(pojoClass, (Set<String>)null, true); - } - - /** - * @param pojoClass The class to convert - * @param excluded The fields to exclude - */ - public JSONPojoConvertor(Class<?> pojoClass, String[] excluded) - { - this(pojoClass, new HashSet<String>(Arrays.asList(excluded)), true); - } - - /** - * @param pojoClass The class to convert - * @param excluded The fields to exclude - */ - public JSONPojoConvertor(Class<?> pojoClass, Set<String> excluded) - { - this(pojoClass, excluded, true); - } - - /** - * @param pojoClass The class to convert - * @param excluded The fields to exclude - * @param fromJSON If true, add a class field to the JSON - */ - public JSONPojoConvertor(Class<?> pojoClass, Set<String> excluded, boolean fromJSON) - { - _pojoClass = pojoClass; - _excluded = excluded; - _fromJSON = fromJSON; - init(); - } - - /** - * @param pojoClass The class to convert - * @param fromJSON If true, add a class field to the JSON - */ - public JSONPojoConvertor(Class<?> pojoClass, boolean fromJSON) - { - this(pojoClass, (Set<String>)null, fromJSON); - } - - /* ------------------------------------------------------------ */ - protected void init() - { - Method[] methods = _pojoClass.getMethods(); - for (int i=0;i<methods.length;i++) - { - Method m=methods[i]; - if (!Modifier.isStatic(m.getModifiers()) && m.getDeclaringClass()!=Object.class) - { - String name=m.getName(); - switch(m.getParameterTypes().length) - { - case 0: - - if(m.getReturnType()!=null) - { - if (name.startsWith("is") && name.length()>2) - name=name.substring(2,3).toLowerCase(Locale.ENGLISH)+name.substring(3); - else if (name.startsWith("get") && name.length()>3) - name=name.substring(3,4).toLowerCase(Locale.ENGLISH)+name.substring(4); - else - break; - if(includeField(name, m)) - addGetter(name, m); - } - break; - case 1: - if (name.startsWith("set") && name.length()>3) - { - name=name.substring(3,4).toLowerCase(Locale.ENGLISH)+name.substring(4); - if(includeField(name, m)) - addSetter(name, m); - } - break; - } - } - } - } - - /* ------------------------------------------------------------ */ - protected void addGetter(String name, Method method) - { - _getters.put(name, method); - } - - /* ------------------------------------------------------------ */ - protected void addSetter(String name, Method method) - { - _setters.put(name, new Setter(name, method)); - } - - /* ------------------------------------------------------------ */ - protected Setter getSetter(String name) - { - return _setters.get(name); - } - - /* ------------------------------------------------------------ */ - protected boolean includeField(String name, Method m) - { - return _excluded==null || !_excluded.contains(name); - } - - /* ------------------------------------------------------------ */ - protected int getExcludedCount() - { - return _excluded==null ? 0 : _excluded.size(); - } - - /* ------------------------------------------------------------ */ - public Object fromJSON(Map object) - { - Object obj = null; - try - { - obj = _pojoClass.newInstance(); - } - catch(Exception e) - { - // TODO return Map instead? - throw new RuntimeException(e); - } - - setProps(obj, object); - return obj; - } - - /* ------------------------------------------------------------ */ - public int setProps(Object obj, Map<?,?> props) - { - int count = 0; - for(Iterator<?> iterator = props.entrySet().iterator(); iterator.hasNext();) - { - Map.Entry<?, ?> entry = (Map.Entry<?,?>) iterator.next(); - Setter setter = getSetter((String)entry.getKey()); - if(setter!=null) - { - try - { - setter.invoke(obj, entry.getValue()); - count++; - } - catch(Exception e) - { - // TODO throw exception? - LOG.warn(_pojoClass.getName()+"#"+setter.getPropertyName()+" not set from "+ - (entry.getValue().getClass().getName())+"="+entry.getValue().toString()); - log(e); - } - } - } - return count; - } - - /* ------------------------------------------------------------ */ - public void toJSON(Object obj, Output out) - { - if(_fromJSON) - out.addClass(_pojoClass); - for(Map.Entry<String,Method> entry : _getters.entrySet()) - { - try - { - out.add(entry.getKey(), entry.getValue().invoke(obj, GETTER_ARG)); - } - catch(Exception e) - { - // TODO throw exception? - LOG.warn("{} property '{}' excluded. (errors)", _pojoClass.getName(), - entry.getKey()); - log(e); - } - } - } - - /* ------------------------------------------------------------ */ - protected void log(Throwable t) - { - LOG.ignore(t); - } - - /* ------------------------------------------------------------ */ - public static class Setter - { - protected String _propertyName; - protected Method _setter; - protected NumberType _numberType; - protected Class<?> _type; - protected Class<?> _componentType; - - public Setter(String propertyName, Method method) - { - _propertyName = propertyName; - _setter = method; - _type = method.getParameterTypes()[0]; - _numberType = __numberTypes.get(_type); - if(_numberType==null && _type.isArray()) - { - _componentType = _type.getComponentType(); - _numberType = __numberTypes.get(_componentType); - } - } - - public String getPropertyName() - { - return _propertyName; - } - - public Method getMethod() - { - return _setter; - } - - public NumberType getNumberType() - { - return _numberType; - } - - public Class<?> getType() - { - return _type; - } - - public Class<?> getComponentType() - { - return _componentType; - } - - public boolean isPropertyNumber() - { - return _numberType!=null; - } - - public void invoke(Object obj, Object value) throws IllegalArgumentException, - IllegalAccessException, InvocationTargetException - { - if(value==null) - _setter.invoke(obj, NULL_ARG); - else - invokeObject(obj, value); - } - - protected void invokeObject(Object obj, Object value) throws IllegalArgumentException, - IllegalAccessException, InvocationTargetException - { - - if (_type.isEnum()) - { - if (value instanceof Enum) - _setter.invoke(obj, new Object[]{value}); - else - _setter.invoke(obj, new Object[]{Enum.valueOf((Class<? extends Enum>)_type,value.toString())}); - } - else if(_numberType!=null && value instanceof Number) - { - _setter.invoke(obj, new Object[]{_numberType.getActualValue((Number)value)}); - } - else if (Character.TYPE.equals(_type) || Character.class.equals(_type)) - { - _setter.invoke(obj, new Object[]{String.valueOf(value).charAt(0)}); - } - else if(_componentType!=null && value.getClass().isArray()) - { - if(_numberType==null) - { - int len = Array.getLength(value); - Object array = Array.newInstance(_componentType, len); - try - { - System.arraycopy(value, 0, array, 0, len); - } - catch(Exception e) - { - // unusual array with multiple types - LOG.ignore(e); - _setter.invoke(obj, new Object[]{value}); - return; - } - _setter.invoke(obj, new Object[]{array}); - } - else - { - Object[] old = (Object[])value; - Object array = Array.newInstance(_componentType, old.length); - try - { - for(int i=0; i<old.length; i++) - Array.set(array, i, _numberType.getActualValue((Number)old[i])); - } - catch(Exception e) - { - // unusual array with multiple types - LOG.ignore(e); - _setter.invoke(obj, new Object[]{value}); - return; - } - _setter.invoke(obj, new Object[]{array}); - } - } - else - _setter.invoke(obj, new Object[]{value}); - } - } - - public interface NumberType - { - public Object getActualValue(Number number); - } - - public static final NumberType SHORT = new NumberType() - { - public Object getActualValue(Number number) - { - return new Short(number.shortValue()); - } - }; - - public static final NumberType INTEGER = new NumberType() - { - public Object getActualValue(Number number) - { - return new Integer(number.intValue()); - } - }; - - public static final NumberType FLOAT = new NumberType() - { - public Object getActualValue(Number number) - { - return new Float(number.floatValue()); - } - }; - - public static final NumberType LONG = new NumberType() - { - public Object getActualValue(Number number) - { - return number instanceof Long ? number : new Long(number.longValue()); - } - }; - - public static final NumberType DOUBLE = new NumberType() - { - public Object getActualValue(Number number) - { - return number instanceof Double ? number : new Double(number.doubleValue()); - } - }; - - static - { - __numberTypes.put(Short.class, SHORT); - __numberTypes.put(Short.TYPE, SHORT); - __numberTypes.put(Integer.class, INTEGER); - __numberTypes.put(Integer.TYPE, INTEGER); - __numberTypes.put(Long.class, LONG); - __numberTypes.put(Long.TYPE, LONG); - __numberTypes.put(Float.class, FLOAT); - __numberTypes.put(Float.TYPE, FLOAT); - __numberTypes.put(Double.class, DOUBLE); - __numberTypes.put(Double.TYPE, DOUBLE); - } -}
--- a/src/org/eclipse/jetty/util/ajax/JSONPojoConvertorFactory.java Wed Sep 07 21:21:32 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,110 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.util.ajax; - -import java.util.Map; - -import org.eclipse.jetty.util.Loader; -import org.eclipse.jetty.util.ajax.JSON.Convertor; -import org.eclipse.jetty.util.ajax.JSON.Output; - -public class JSONPojoConvertorFactory implements JSON.Convertor -{ - private final JSON _json; - private final boolean _fromJson; - - public JSONPojoConvertorFactory(JSON json) - { - if (json==null) - { - throw new IllegalArgumentException(); - } - _json=json; - _fromJson=true; - } - - /* ------------------------------------------------------------ */ - /** - * @param json The JSON instance to use - * @param fromJSON If true, the class name of the objects is included - * in the generated JSON and is used to instantiate the object when - * JSON is parsed (otherwise a Map is used). - */ - public JSONPojoConvertorFactory(JSON json,boolean fromJSON) - { - if (json==null) - { - throw new IllegalArgumentException(); - } - _json=json; - _fromJson=fromJSON; - } - - /* ------------------------------------------------------------ */ - public void toJSON(Object obj, Output out) - { - String clsName=obj.getClass().getName(); - Convertor convertor=_json.getConvertorFor(clsName); - if (convertor==null) - { - try - { - Class cls=Loader.loadClass(JSON.class,clsName); - convertor=new JSONPojoConvertor(cls,_fromJson); - _json.addConvertorFor(clsName, convertor); - } - catch (ClassNotFoundException e) - { - JSON.LOG.warn(e); - } - } - if (convertor!=null) - { - convertor.toJSON(obj, out); - } - } - - public Object fromJSON(Map object) - { - Map map=object; - String clsName=(String)map.get("class"); - if (clsName!=null) - { - Convertor convertor=_json.getConvertorFor(clsName); - if (convertor==null) - { - try - { - Class cls=Loader.loadClass(JSON.class,clsName); - convertor=new JSONPojoConvertor(cls,_fromJson); - _json.addConvertorFor(clsName, convertor); - } - catch (ClassNotFoundException e) - { - JSON.LOG.warn(e); - } - } - if (convertor!=null) - { - return convertor.fromJSON(object); - } - } - return map; - } -}