本文整理汇总了Java中android.util.JsonToken类的典型用法代码示例。如果您正苦于以下问题:Java JsonToken类的具体用法?Java JsonToken怎么用?Java JsonToken使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonToken类属于android.util包,在下文中一共展示了JsonToken类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import android.util.JsonToken; //导入依赖的package包/类
@Override public Integer parse(JsonReader reader, float scale) throws IOException {
boolean isArray = reader.peek() == JsonToken.BEGIN_ARRAY;
if (isArray) {
reader.beginArray();
}
double r = reader.nextDouble();
double g = reader.nextDouble();
double b = reader.nextDouble();
double a = reader.nextDouble();
if (isArray) {
reader.endArray();
}
if (r <= 1 && g <= 1 && b <= 1 && a <= 1) {
r *= 255;
g *= 255;
b *= 255;
a *= 255;
}
return Color.argb((int) a, (int) r, (int) g, (int) b);
}
示例2: parse
import android.util.JsonToken; //导入依赖的package包/类
@Override public PointF parse(JsonReader reader, float scale) throws IOException {
JsonToken token = reader.peek();
if (token == JsonToken.BEGIN_ARRAY) {
return JsonUtils.jsonToPoint(reader, scale);
} else if (token == JsonToken.BEGIN_OBJECT) {
return JsonUtils.jsonToPoint(reader, scale);
} else if (token == JsonToken.NUMBER) {
// This is the case where the static value for a property is an array of numbers.
// We begin the array to see if we have an array of keyframes but it's just an array
// of static numbers instead.
PointF point = new PointF((float) reader.nextDouble() * scale, (float) reader.nextDouble() * scale);
while (reader.hasNext()) {
reader.skipValue();
}
return point;
} else {
throw new IllegalArgumentException("Cannot convert json to point. Next token is " + token);
}
}
示例3: valueFromObject
import android.util.JsonToken; //导入依赖的package包/类
static float valueFromObject(JsonReader reader) throws IOException {
JsonToken token = reader.peek();
switch (token) {
case NUMBER:
return (float) reader.nextDouble();
case BEGIN_ARRAY:
reader.beginArray();
float val = (float) reader.nextDouble();
while (reader.hasNext()) {
reader.skipValue();
}
reader.endArray();
return val;
default:
throw new IllegalArgumentException("Unknown value for token of type " + token);
}
}
示例4: readValue
import android.util.JsonToken; //导入依赖的package包/类
/** Reads the next value in the {@link JsonReader}. */
private static Object readValue(JsonReader reader) throws IOException {
JsonToken token = reader.peek();
switch (token) {
case BEGIN_OBJECT:
return readerToMap(reader);
case BEGIN_ARRAY:
return readerToList(reader);
case BOOLEAN:
return reader.nextBoolean();
case NULL:
reader.nextNull(); // consume the null token
return null;
case NUMBER:
return reader.nextDouble();
case STRING:
return reader.nextString();
default:
throw new IllegalStateException("Invalid token " + token);
}
}
示例5: readNext
import android.util.JsonToken; //导入依赖的package包/类
@Override
protected Event readNext() throws IOException {
if (!reader.hasNext()) {
return null;
}
if (reader.peek() == JsonToken.END_ARRAY) {
return null;
}
try {
return parseRecord();
} catch (IOException e) {
throw new IOException(getExceptionText(R.string.restore_json_corrupted), e);
}
}
示例6: deserializeStringCollection
import android.util.JsonToken; //导入依赖的package包/类
/**
* A generic deserializer for string collections.
*
* @param serialized JSON representation of a string collection.
* @param collectionClass Concrete, instantiatable collection class, e.g. {@code ArrayList.class}.
* @param <C> A concrete collection class, e.g. {@code ArrayList<String>}.
* @return A collection instance retrieved from {@code serialized}.
*/
@NonNull
public static <C extends Collection<String>> C deserializeStringCollection(@NonNull String serialized,
@NonNull Class<?> collectionClass) {
C collection = createCollection(collectionClass);
StringReader reader = new StringReader(serialized);
JsonReader jsonReader = new JsonReader(reader);
try {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) {
jsonReader.nextNull();
collection.add(null);
} else {
collection.add(jsonReader.nextString());
}
}
jsonReader.endArray();
jsonReader.close();
return collection;
} catch (IOException e) {
return collection;
}
}
示例7: insertJournal
import android.util.JsonToken; //导入依赖的package包/类
private void insertJournal(JsonReader jsonReader, Journal journal) throws IOException {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String key = jsonReader.nextName();
if(jsonReader.peek() == JsonToken.NULL) continue;
if (key.equals(KEY_ID)) journal.setId(jsonReader.nextLong());
else if (key.equals(KEY_DATE)) journal.setDate(jsonReader.nextLong());
else if (key.equals(KEY_NOTE)) journal.setNote(jsonReader.nextString());
else if (key.equals(KEY_AMOUNT)) journal.setAmount(jsonReader.nextDouble());
else if (key.equals(KEY_CREATED_DATE)) journal.setCreatedDate(jsonReader.nextLong());
else if (key.equals(KEY_TYPE)) journal.setType(Journal.Type.valueOf(jsonReader.nextString()));
else if (key.equals(KEY_ATTACHMENTS)) {
//this logic assumes that key_attachments comes at the end
long newId = mServices.addJournal(journal);
insertAttachmentsIntoDB(jsonReader, newId);
}
else jsonReader.skipValue();
}
jsonReader.endObject();
}
示例8: insertAttachment
import android.util.JsonToken; //导入依赖的package包/类
private void insertAttachment(JsonReader jsonReader, Attachment attachment) throws IOException {
if(jsonReader.peek() == JsonToken.NAME) {
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String key = jsonReader.nextName();
if (jsonReader.peek() == JsonToken.NULL) continue;
if (key.equals(KEY_ID)) attachment.setId(jsonReader.nextLong());
else if (key.equals(KEY_PATH)) attachment.setPath(jsonReader.nextString());
else jsonReader.skipValue();
}
mServices.addAttachment(attachment);
jsonReader.endObject();
}else{
//for old json format
while (jsonReader.hasNext()) {
if (jsonReader.peek() == JsonToken.NULL) continue;
else attachment.setPath(jsonReader.nextString());
}
mServices.addAttachment(attachment);
}
}
示例9: inflate
import android.util.JsonToken; //导入依赖的package包/类
/***
* This method called in the background, here
* need to parse given {@code json}
*
* @param reader Widget configuration
*/
protected void inflate(ConfigurationWidget widget, JsonReader reader) throws IOException {
Bundle options = new Bundle();
Bundle configs = widget.getConfigurations();
JsonToken token = reader.peek();
if(token == JsonToken.BEGIN_OBJECT){
appendViewInLayout(createView(this, configs, options, reader));
}else if (token == JsonToken.BEGIN_ARRAY){
reader.beginArray();
while(reader.peek() != JsonToken.END_ARRAY){
appendViewInLayout(createView(this, configs, options, reader));
options.clear(); // recycle configurations
}
reader.endArray();
}else{
throw new RuntimeException("Unsupported token exception: " + reader.toString());
}
options.clear();
}
示例10: handleSystemOptions
import android.util.JsonToken; //导入依赖的package包/类
private static boolean handleSystemOptions(Bundle options, String key, JsonReader reader){
try {
if (key.equals("entities")) {
reader.beginObject();
ArrayList<String> entities = new ArrayList<>();
while(reader.peek() != JsonToken.END_OBJECT){
reader.skipValue(); //skip name
entities.add(reader.nextString());
}
reader.endObject();
options.putStringArrayList(key, entities);
return true;
}
}catch (IOException e){
e.printStackTrace();
}
return false;
}
示例11: parseAsJsonObject
import android.util.JsonToken; //导入依赖的package包/类
/**
* Parse the next value as an object, but do not attempt to convert it or any children to a
* known type. The returned object will be a {@link JSONObject} and all children will be
* JSONObjects, JSONArrays, and primitives.
*
* @param reader The JsonReader to use. Calls to {@link JsonReader#beginObject()} and {@link
* JsonReader#endObject()} will be taken care of by this method.
* @param key The key corresponding to the current value. This is used to make more useful error
* messages.
*/
public static JSONObject parseAsJsonObject(JsonReader reader, String key) throws IOException {
if (handleNull(reader)) {
return null;
}
assertType(reader, key, JsonToken.BEGIN_OBJECT);
JSONObject result = new JSONObject();
reader.beginObject();
while (reader.hasNext()) {
try {
result.put(reader.nextName(), parseNextValue(reader, false));
} catch (JSONException e) {
throw new RuntimeException("This should be impossible.", e);
}
}
reader.endObject();
return result;
}
示例12: readNGWFeatureAttachments
import android.util.JsonToken; //导入依赖的package包/类
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static void readNGWFeatureAttachments(Feature feature, JsonReader reader)
throws IOException
{
//add extensions
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (name.equals("attachment") && reader.peek() != JsonToken.NULL) {
reader.beginArray();
while (reader.hasNext()) {
readNGWFeatureAttachment(feature, reader);
}
reader.endArray();
} else {
reader.skipValue();
}
}
reader.endObject();
}
示例13: createUsingJsonStream
import android.util.JsonToken; //导入依赖的package包/类
@SuppressWarnings("cast")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static some.test.Simple createUsingJsonStream(Realm realm, JsonReader reader)
throws IOException {
final some.test.Simple obj = new some.test.Simple();
final SimpleRealmProxyInterface objProxy = (SimpleRealmProxyInterface) obj;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
if (false) {
} else if (name.equals("name")) {
if (reader.peek() != JsonToken.NULL) {
objProxy.realmSet$name((String) reader.nextString());
} else {
reader.skipValue();
objProxy.realmSet$name(null);
}
} else if (name.equals("age")) {
if (reader.peek() != JsonToken.NULL) {
objProxy.realmSet$age((int) reader.nextInt());
} else {
reader.skipValue();
throw new IllegalArgumentException("Trying to set non-nullable field 'age' to null.");
}
} else {
reader.skipValue();
}
}
reader.endObject();
return realm.copyToRealm(obj);
}
示例14: readParentComment
import android.util.JsonToken; //导入依赖的package包/类
private ParentComment readParentComment() throws IOException {
String content = null;
String author = null;
String link = null;
reader.beginObject();
while (reader.hasNext()) {
String name = reader.nextName();
JsonToken check = reader.peek();
if (check == JsonToken.NULL) {
reader.skipValue();
continue;
}
switch (name) {
case "author":
author = formatUsername(reader.nextString());
break;
case "orig_body":
content = reader.nextString().replace("^", "");
break;
case "link":
link = reader.nextString();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();
return new ParentComment(content, author, link);
}
示例15: AnnouncementsScraper
import android.util.JsonToken; //导入依赖的package包/类
public AnnouncementsScraper(Blackboard blackboard) {
super(blackboard);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
if (!isCancelled()) {
getBlackboard().getHtmlContent("announcementList", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
JsonReader reader = new JsonReader(new StringReader(s));
reader.setLenient(true);
try {
if (reader.peek() != JsonToken.NULL) {
if (reader.peek() == JsonToken.STRING)
onComplete(reader.nextString());
}
} catch (Exception ignored) {
}
try {
reader.close();
} catch (IOException ignored) {
}
if (!isComplete()) {
onError(false);
handler.postDelayed(runnable, 1000);
}
}
});
}
}
};
}