Back to project page JsonPullParser.
The source code is released under:
Apache License
If you think the Android project JsonPullParser listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright 2011 vvakame <vvakame@gmail.com> */*from w ww. j a va2 s. c o m*/ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.vvakame.sample.converter; import java.io.IOException; import java.io.Writer; 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; import net.vvakame.util.jsonpullparser.util.TokenConverter; /** * Converts string (in JSON form) into {@link String}, or vice versa. * @author vvakame */ public class StringDiscardConverter extends TokenConverter<String> { /** * Returns an instance of {@link StringDiscardConverter}. * @return {@link StringDiscardConverter} * @author vvakame */ public static StringDiscardConverter getInstance() { return new StringDiscardConverter(); } /** * {@inheritDoc} */ @Override public String parse(JsonPullParser parser, OnJsonObjectAddListener listener) throws IOException, JsonFormatException { if (parser == null) { throw new IllegalArgumentException(); } State state = parser.getEventType(); switch (state) { case VALUE_NULL: return null; case VALUE_STRING: return parser.getValueString(); default: throw new IllegalStateException(); } } /** * {@inheritDoc} */ @Override public void encodeNullToNull(Writer writer, String obj) throws IOException { JsonUtil.put(writer, obj); } }