All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sonpullparser-apt.1.6.2.source-code.JsonModelGen.mvel Maven / Gradle / Ivy

The newest version!
package @{packageName};

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import net.vvakame.util.jsonpullparser.JsonFormatException;
import net.vvakame.util.jsonpullparser.JsonPullParser;
import net.vvakame.util.jsonpullparser.JsonPullParser.State;
import net.vvakame.util.jsonpullparser.util.JsonUtil;
import net.vvakame.util.jsonpullparser.util.OnJsonObjectAddListener;
@if{storeJsonElement.storeJson == true}
import net.vvakame.util.jsonpullparser.util.JsonSliceUtil;
@end{}

/**
 * Parses JSON-formatted data into {@link @{targetNew}} with {@link JsonPullParser}.
* NB: This class is auto-generated by APT; please do not edit by hand. */ @if{genToPackagePrivate != true}public @end{}class @{target}@{postfix} { /** * Attempts to parse the given data as {@link List} of {@link @{targetNew}}. * @param json JSON-formatted data * @return {@link List} of {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static List<@{targetNew}> getList(String json) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(json); return getList(parser, null); } /** * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes. * * @param json JSON-formatted data * @param listener {@link OnJsonObjectAddListener} to notify * @return {@link List} of {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static List<@{targetNew}> getList(String json, OnJsonObjectAddListener listener) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(json); return getList(parser, listener); } /** * Attempts to parse the given data as {@link List} of {@link @{targetNew}}. * * @param stream JSON-formatted data * @return {@link List} of {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static List<@{targetNew}> getList(InputStream stream) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(stream); return getList(parser, null); } /** * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes. * * @param stream JSON-formatted data * @param listener {@link OnJsonObjectAddListener} to notify * @return {@link List} of {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static List<@{targetNew}> getList(InputStream stream, OnJsonObjectAddListener listener) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(stream); return getList(parser, listener); } /** * Attempts to parse the given data as {@link List} of {@link @{targetNew}}. * * @param parser {@link JsonPullParser} with some JSON-formatted data * @return {@link List} of {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static List<@{targetNew}> getList(JsonPullParser parser) throws IOException, JsonFormatException { return getList(parser, null); } /** * Attempts to parse the given data as {@link List} of {@link @{targetNew}}.
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes. * * @param parser {@link JsonPullParser} with some JSON-formatted data * @param listener {@link OnJsonObjectAddListener} to notify * @return {@link List} of {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static List<@{targetNew}> getList(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, JsonFormatException { List<@{targetNew}> list = new ArrayList<@{targetNew}>(); State eventType = parser.getEventType(); if (eventType == State.VALUE_NULL) { if (listener != null) { listener.onAdd(null); } return null; } if (eventType != State.START_ARRAY) { if(eventType == State.START_HASH){ throw new JsonFormatException("not started '['!, Do you want the json hash?"); } else { throw new JsonFormatException("not started '['!"); } } while (parser.lookAhead() != State.END_ARRAY) { @{targetNew} tmp = get(parser, listener); list.add(tmp); } parser.getEventType(); return list; } /** * Attempts to parse the given data as {@link @{targetNew}}. * * @param json JSON-formatted data * @return {{@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static @{targetNew} get(String json) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(json); return get(parser, null); } /** * Attempts to parse the given data as {@link @{targetNew}}.
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes. * * @param json JSON-formatted data * @param listener {@link OnJsonObjectAddListener} to notify * @return {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static @{targetNew} get(String json, OnJsonObjectAddListener listener) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(json); return get(parser, listener); } /** * Attempts to parse the given data as {@link @{targetNew}}. * * @param stream JSON-formatted data * @return {{@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static @{targetNew} get(InputStream stream) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(stream); return get(parser, null); } /** * Attempts to parse the given data as {@link @{targetNew}}.
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes. * * @param stream JSON-formatted data * @param listener {@link OnJsonObjectAddListener} to notify * @return {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static @{targetNew} get(InputStream stream, OnJsonObjectAddListener listener) throws IOException, JsonFormatException { JsonPullParser parser = JsonPullParser.newParser(stream); return get(parser, listener); } /** * Attempts to parse the given data as {@link @{targetNew}}. * * @param parser {@link JsonPullParser} with some JSON-formatted data * @return {{@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static @{targetNew} get(JsonPullParser parser) throws IOException, JsonFormatException { return get(parser, null); } /** * Attempts to parse the given data as {@link @{targetNew}}.
* Accepts {@link OnJsonObjectAddListener}; allows you to peek various intermittent instances as parsing goes. * * @param parser {@link JsonPullParser} with some JSON-formatted data * @param listener {@link OnJsonObjectAddListener} to notify * @return {@link @{targetNew}} * @throws IOException if an I/O error occurs. * @throws IllegalStateException @SaveOrigin is enabled while {@link JsonPullParser#setLogEnable()} is not called yet. * @throws JsonFormatException The given data is malformed, or its type is unexpected */ public static @{targetNew} get(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, IllegalStateException, JsonFormatException { @if{storeJsonElement.storeJson == true} int start = -1; @if{storeJsonElement.treatLogDisabledAsError == true} if(parser.isLogEnabled()){ start = parser.getSliceSize(); } else { throw new IllegalStateException("parser is not log enabled."); } @else{} if(parser.isLogEnabled()){ start = parser.getSliceSize(); } @end{} @end{} @{targetNew} obj = new @{targetNew}(); State eventType = parser.getEventType(); if (eventType == State.VALUE_NULL) { if (listener != null) { listener.onAdd(null); } return null; } if (eventType != State.START_HASH) { if (eventType == State.START_ARRAY) { throw new JsonFormatException("not started '{'! Do you want the json array?"); } else { throw new JsonFormatException("not started '{'!"); } } while ((eventType = parser.getEventType()) != State.END_HASH) { if (eventType != State.KEY) { throw new JsonFormatException("expect KEY. we got unexpected value. " + eventType); } String key = parser.getValueString(); if(parseValue(parser, listener, key, obj)){ continue; @if{treatUnknownKeyAsError == true} } else { throw new JsonFormatException("unsupported key. key=" + key); } @else{} } else { parser.discardValue(); } @end{} } @if{storeJsonElement.storeJson == true} int end; if(parser.isLogEnabled()){ end = parser.getSliceSize(); obj.@{storeJsonElement.setter}(JsonSliceUtil.slicesToString(parser.getSlices().subList(start, end))); } @end{} if (listener != null) { listener.onAdd(obj); } return obj; } /** * *** Internal use only; please do not call directly. ***
* *** Public access granted only to allow reusing from other packages. *** * * @param parser {@link JsonPullParser} in action * @param listener {@link OnJsonObjectAddListener} to listen new instance creations * @param key JSON key being parsed * @param obj {@link @{targetNew}} (and like) being rebuilt * @return True if the given key is parsed successfully, false otherwise * @throws IOException if an I/O error occurs. * @throws JsonFormatException Data is malformed, or its type is unexpected * @author vvakame */ public static boolean parseValue(JsonPullParser parser, OnJsonObjectAddListener listener, String key, @{targetNew} obj) throws IOException, JsonFormatException { @foreach{obj : inElements} if ("@{obj.key}".equals(key)) { @if{obj.kind == "BOOLEAN"} parser.getEventType(); obj.@{obj.setter}(parser.getValueBoolean()); @else{obj.kind == "CHAR"} parser.getEventType(); obj.@{obj.setter}(parser.getValueString().charAt(0)); @else{obj.kind == "BYTE"} parser.getEventType(); obj.@{obj.setter}((byte)parser.getValueLong()); @else{obj.kind == "SHORT"} parser.getEventType(); obj.@{obj.setter}((short)parser.getValueLong()); @else{obj.kind == "INT"} parser.getEventType(); obj.@{obj.setter}((int)parser.getValueLong()); @else{obj.kind == "LONG"} parser.getEventType(); obj.@{obj.setter}(parser.getValueLong()); @else{obj.kind == "FLOAT"} parser.getEventType(); obj.@{obj.setter}((float)parser.getValueDouble()); @else{obj.kind == "DOUBLE"} parser.getEventType(); obj.@{obj.setter}(parser.getValueDouble()); @else{obj.kind == "STRING"} parser.getEventType(); obj.@{obj.setter}(parser.getValueString()); @else{obj.kind == "LIST"} @if{obj.modelName == "java.lang.Boolean"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserBooleanList(parser)); @else{obj.modelName == "java.lang.Integer"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserIntegerList(parser)); @else{obj.modelName == "java.lang.Long"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserLongList(parser)); @else{obj.modelName == "java.lang.Byte"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserByteList(parser)); @else{obj.modelName == "java.lang.Short"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserShortList(parser)); @else{obj.modelName == "java.lang.Character"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserCharacterList(parser)); @else{obj.modelName == "java.lang.Double"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserDoubleList(parser)); @else{obj.modelName == "java.lang.Float"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserFloatList(parser)); @else{obj.modelName == "java.lang.String"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserStringList(parser)); @else{obj.modelName == "java.util.Date"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserDateList(parser)); @else{obj.subKind == "ENUM"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserEnumList(parser, @{obj.modelName}.class)); @else{} obj.@{obj.setter}(@{obj.genName}@{postfix}.getList(parser, listener)); @end{} @else{obj.kind == "BOOLEAN_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserBoolean(parser)); @else{obj.kind == "DOUBLE_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserDouble(parser)); @else{obj.kind == "LONG_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserLong(parser)); @else{obj.kind == "BYTE_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserByte(parser)); @else{obj.kind == "CHAR_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserCharacter(parser)); @else{obj.kind == "FLOAT_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserFloat(parser)); @else{obj.kind == "INT_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserInteger(parser)); @else{obj.kind == "SHORT_WRAPPER"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonParseUtil.parserShort(parser)); @else{obj.kind == "ENUM"} if(State.VALUE_NULL.equals(parser.getEventType())){ obj.@{obj.setter}(null); } else { obj.@{obj.setter}(Enum.valueOf(@{obj.modelName}.class, parser.getValueString())); } @else{obj.kind == "DATE"} if(State.VALUE_NULL.equals(parser.getEventType())){ obj.@{obj.setter}(null); } else { obj.@{obj.setter}(new java.util.Date(parser.getValueLong())); } @else{obj.kind == "JSON_ARRAY"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonArray.fromParser(parser)); @else{obj.kind == "JSON_HASH"} obj.@{obj.setter}(net.vvakame.util.jsonpullparser.util.JsonHash.fromParser(parser)); @else{obj.kind == "MODEL"} @{obj.modelName} tmp = @{obj.genName}@{postfix}.get(parser, listener); obj.@{obj.setter}(tmp); @else{obj.kind == "CONVERTER"} @{obj.converter} converter = @{obj.converter}.getInstance(); @{obj.modelName} tmp = converter.parse(parser, listener); obj.@{obj.setter}(tmp); @end{} @end{' } else '} @if{existsBase == true} } else if(@{targetBase}@{postfix}.parseValue(parser, listener, key, obj)) { return true; @end{} } else { return false; } return true; } /** * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and appends it into the given stream using {@link JsonPullParser#DEFAULT_CHARSET}.
* This method is an alias of {@link #encodeListNullToBlank(Writer, List)}. * * @param out {@link OutputStream} to be written * @param list {@link List} of {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encodeList(OutputStream out, List list) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(out, JsonPullParser.DEFAULT_CHARSET); encodeListNullToBlank(writer, list); } /** * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and writes it using the given writer.
* This method is an alias of {@link #encodeListNullToBlank(Writer, List)}. * * @param writer {@link Writer} to be used for writing value * @param list {@link List} of {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encodeList(Writer writer, List list) throws IOException { encodeListNullToBlank(writer, list); } /** * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and writes it using the given writer.
* Writes "[]" if null is given. * * @param writer {@link Writer} to be used for writing value * @param list {@link List} of {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encodeListNullToBlank(Writer writer, List list) throws IOException { if (list == null) { writer.write("[]"); writer.flush(); return; } encodeListNullToNull(writer, list); } /** * Encodes the given {@link List} of {@link @{targetNew}} into the JSON format, and writes it using the given writer.
* Writes "null" if null is given. * * @param writer {@link Writer} to be used for writing value * @param list {@link List} of {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encodeListNullToNull(Writer writer, List list) throws IOException { if (list == null) { writer.write("null"); writer.flush(); return; } JsonUtil.startArray(writer); int size = list.size(); for (int i = 0; i < size; i++) { encodeNullToNull(writer, list.get(i)); if (i + 1 < size) { JsonUtil.addSeparator(writer); } } JsonUtil.endArray(writer); writer.flush(); } /** * Encodes the given {@link @{targetNew}} into the JSON format, and appends it into the given stream using {@link JsonPullParser#DEFAULT_CHARSET}.
* This method is an alias of {@link #encodeNullToBlank(Writer, @{targetNew})}. * * @param out {@link OutputStream} to be written * @param obj {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encode(OutputStream out, @{targetNew} obj) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(out, JsonPullParser.DEFAULT_CHARSET); encodeNullToBlank(writer, obj); } /** * Encodes the given {@link @{targetNew}} into the JSON format, and writes it using the given writer.
* This method is an alias of {@link #encodeNullToBlank(Writer, @{targetNew})}. * * @param writer {@link Writer} to be used for writing value * @param obj {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encode(Writer writer, @{targetNew} obj) throws IOException { encodeNullToBlank(writer, obj); } /** * Encodes the given {@link @{targetNew}} into the JSON format, and writes it using the given writer.
* Writes "{}" if null is given. * * @param writer {@link Writer} to be used for writing value * @param obj {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encodeNullToBlank(Writer writer, @{targetNew} obj) throws IOException { if (obj == null) { writer.write("{}"); writer.flush(); return; } encodeNullToNull(writer, obj); } /** * Encodes the given {@link @{targetNew}} into the JSON format, and writes it using the given writer.
* Writes "null" if null is given. * * @param writer {@link Writer} to be used for writing value * @param obj {@link @{targetNew}} to be encoded * @throws IOException if an I/O error occurs. */ public static void encodeNullToNull(Writer writer, @{targetNew} obj) throws IOException { if (obj == null) { writer.write("null"); return; } JsonUtil.startHash(writer); encodeValue(writer, obj); JsonUtil.endHash(writer); writer.flush(); } /** * *** Internal use only; please do not call directly. ***
* *** Public access granted only to allow reusing from other packages. *** * * @param writer {@link Writer} * @param obj value * @return is write some value * @throws IOException if an I/O error occurs. * @author vvakame */ public static boolean encodeValue(Writer writer, @{targetNew} obj) throws IOException { @if{existsBase == true} @if{outElements.size() == 0} boolean existParentValue = false; @end{} if(@{targetBase}@{postfix}.encodeValue(writer, obj)){ @if{outElements.size() != 0} JsonUtil.addSeparator(writer); @else{} existParentValue = true; @end{} } @end{} @foreach{obj : outElements} JsonUtil.putKey(writer, "@{obj.key}"); @if{obj.kind == "LIST"} @if{obj.modelName == "java.lang.Boolean"} JsonUtil.putBooleanList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Integer"} JsonUtil.putIntegerList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Long"} JsonUtil.putLongList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Byte"} JsonUtil.putByteList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Short"} JsonUtil.putShortList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Character"} JsonUtil.putCharacterList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Double"} JsonUtil.putDoubleList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.Float"} JsonUtil.putFloatList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.lang.String"} JsonUtil.putStringList(writer, obj.@{obj.getter}()); @else{obj.modelName == "java.util.Date"} JsonUtil.putDateList(writer, obj.@{obj.getter}()); @else{obj.subKind == "ENUM"} JsonUtil.putEnumList(writer, obj.@{obj.getter}()); @else{} @{obj.genName}@{postfix}.encodeListNullToNull(writer, obj.@{obj.getter}()); @end{} @else{obj.kind == "BOOLEAN_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "DOUBLE_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "LONG_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "BYTE_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "CHAR_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "FLOAT_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "INT_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "SHORT_WRAPPER"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "ENUM"} JsonUtil.put(writer, obj.@{obj.getter}()); @else{obj.kind == "JSON_ARRAY"} if(obj.@{obj.getter}() != null){ obj.@{obj.getter}().toJson(writer); } else { JsonUtil.put(writer, (Object) null); } @else{obj.kind == "JSON_HASH"} if(obj.@{obj.getter}() != null){ obj.@{obj.getter}().toJson(writer); } else { JsonUtil.put(writer, (Object) null); } @else{obj.kind == "MODEL"} @{obj.genName}@{postfix}.encodeNullToNull(writer, obj.@{obj.getter}()); @else{obj.kind == "CONVERTER"} @{obj.converter}.getInstance().encodeNullToNull(writer, obj.@{obj.getter}()); @else{} JsonUtil.put(writer, obj.@{obj.getter}()); @end{} @end{' JsonUtil.addSeparator(writer);'} @if{existsBase == true} @if{outElements.size() == 0} return existParentValue; @else{} return true; @end{} @else{} @if{outElements.size() == 0} return false; @else{} return true; @end{} @end{} } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy