net.vvakame.zaim4j.MoneyStampGen Maven / Gradle / Ivy
package net.vvakame.zaim4j;
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;
/**
* Parses JSON-formatted data into {@link MoneyStamp} with {@link JsonPullParser}.
* NB: This class is auto-generated by APT; please do not edit by hand.
*/
class MoneyStampGen {
/**
* Attempts to parse the given data as {@link List} of {@link MoneyStamp}.
* @param json JSON-formatted data
* @return {@link List} of {@link MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static List 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 MoneyStamp}.
* 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 MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static List 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 MoneyStamp}.
*
* @param stream JSON-formatted data
* @return {@link List} of {@link MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static List 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 MoneyStamp}.
* 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 MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static List 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 MoneyStamp}.
*
* @param parser {@link JsonPullParser} with some JSON-formatted data
* @return {@link List} of {@link MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static List getList(JsonPullParser parser) throws IOException, JsonFormatException {
return getList(parser, null);
}
/**
* Attempts to parse the given data as {@link List} of {@link MoneyStamp}.
* 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 MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static List getList(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, JsonFormatException {
List list = new ArrayList();
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) {
MoneyStamp tmp = get(parser, listener);
list.add(tmp);
}
parser.getEventType();
return list;
}
/**
* Attempts to parse the given data as {@link MoneyStamp}.
*
* @param json JSON-formatted data
* @return {{@link MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static MoneyStamp get(String json) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(json);
return get(parser, null);
}
/**
* Attempts to parse the given data as {@link MoneyStamp}.
* 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 MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static MoneyStamp 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 MoneyStamp}.
*
* @param stream JSON-formatted data
* @return {{@link MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static MoneyStamp get(InputStream stream) throws IOException, JsonFormatException {
JsonPullParser parser = JsonPullParser.newParser(stream);
return get(parser, null);
}
/**
* Attempts to parse the given data as {@link MoneyStamp}.
* 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 MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static MoneyStamp 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 MoneyStamp}.
*
* @param parser {@link JsonPullParser} with some JSON-formatted data
* @return {{@link MoneyStamp}
* @throws IOException if an I/O error occurs.
* @throws JsonFormatException The given data is malformed, or its type is unexpected
*/
public static MoneyStamp get(JsonPullParser parser) throws IOException, JsonFormatException {
return get(parser, null);
}
/**
* Attempts to parse the given data as {@link MoneyStamp}.
* 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 MoneyStamp}
* @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 MoneyStamp get(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, IllegalStateException, JsonFormatException {
MoneyStamp obj = new MoneyStamp();
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;
} else {
throw new JsonFormatException("unsupported key. key=" + key);
}
}
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 MoneyStamp} (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, MoneyStamp obj) throws IOException, JsonFormatException {
if ("image_url".equals(key)) {
parser.getEventType();
obj.setImageUrl(parser.getValueString());
} else
if ("name".equals(key)) {
parser.getEventType();
obj.setName(parser.getValueString());
} else
if ("description".equals(key)) {
parser.getEventType();
obj.setDescription(parser.getValueString());
} else {
return false;
}
return true;
}
/**
* Encodes the given {@link List} of {@link MoneyStamp} 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 MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encodeList(OutputStream out, List extends MoneyStamp> list) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, JsonPullParser.DEFAULT_CHARSET);
encodeListNullToBlank(writer, list);
}
/**
* Encodes the given {@link List} of {@link MoneyStamp} 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 MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encodeList(Writer writer, List extends MoneyStamp> list) throws IOException {
encodeListNullToBlank(writer, list);
}
/**
* Encodes the given {@link List} of {@link MoneyStamp} 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 MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encodeListNullToBlank(Writer writer, List extends MoneyStamp> list) throws IOException {
if (list == null) {
writer.write("[]");
writer.flush();
return;
}
encodeListNullToNull(writer, list);
}
/**
* Encodes the given {@link List} of {@link MoneyStamp} 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 MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encodeListNullToNull(Writer writer, List extends MoneyStamp> 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 MoneyStamp} 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, MoneyStamp)}.
*
* @param out {@link OutputStream} to be written
* @param obj {@link MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encode(OutputStream out, MoneyStamp obj) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, JsonPullParser.DEFAULT_CHARSET);
encodeNullToBlank(writer, obj);
}
/**
* Encodes the given {@link MoneyStamp} into the JSON format, and writes it using the given writer.
* This method is an alias of {@link #encodeNullToBlank(Writer, MoneyStamp)}.
*
* @param writer {@link Writer} to be used for writing value
* @param obj {@link MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encode(Writer writer, MoneyStamp obj) throws IOException {
encodeNullToBlank(writer, obj);
}
/**
* Encodes the given {@link MoneyStamp} 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 MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encodeNullToBlank(Writer writer, MoneyStamp obj) throws IOException {
if (obj == null) {
writer.write("{}");
writer.flush();
return;
}
encodeNullToNull(writer, obj);
}
/**
* Encodes the given {@link MoneyStamp} 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 MoneyStamp} to be encoded
* @throws IOException if an I/O error occurs.
*/
public static void encodeNullToNull(Writer writer, MoneyStamp 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, MoneyStamp obj) throws IOException {
JsonUtil.putKey(writer, "image_url");
JsonUtil.put(writer, obj.getImageUrl());
JsonUtil.addSeparator(writer);
JsonUtil.putKey(writer, "name");
JsonUtil.put(writer, obj.getName());
JsonUtil.addSeparator(writer);
JsonUtil.putKey(writer, "description");
JsonUtil.put(writer, obj.getDescription());
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy