Gson源码的研读-2015/10/9
来源:互联网 发布:des 算法 函数原理图 编辑:程序博客网 时间:2024/06/02 03:59
当前发现最核心的代码如下:
public static final TypeAdapter<JsonElement> JSON_ELEMENT = new TypeAdapter<JsonElement>() { @Override public JsonElement read(JsonReader in) throws IOException { switch (in.peek()) { case STRING: return new JsonPrimitive(in.nextString()); case NUMBER: String number = in.nextString(); return new JsonPrimitive(new LazilyParsedNumber(number)); case BOOLEAN: return new JsonPrimitive(in.nextBoolean()); case NULL: in.nextNull(); return JsonNull.INSTANCE; case BEGIN_ARRAY: JsonArray array = new JsonArray(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: JsonObject object = new JsonObject(); in.beginObject(); while (in.hasNext()) { object.add(in.nextName(), read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }
以上代码返回各种具体类型的element
下面的代码用于对text继续遍历
/** * Returns the type of the next token without consuming it. */public JsonToken peek() throws IOException { if (token != null) { return token; } switch (stack[stackSize - 1]) { case EMPTY_DOCUMENT: if (lenient) { consumeNonExecutePrefix(); } stack[stackSize - 1] = JsonScope.NONEMPTY_DOCUMENT; JsonToken firstToken = nextValue(); if (!lenient && token != JsonToken.BEGIN_ARRAY && token != JsonToken.BEGIN_OBJECT) { throw new IOException("Expected JSON document to start with '[' or '{' but was " + token + " at line " + getLineNumber() + " column " + getColumnNumber()); } return firstToken; case EMPTY_ARRAY: return nextInArray(true); case NONEMPTY_ARRAY: return nextInArray(false); case EMPTY_OBJECT: return nextInObject(true); case DANGLING_NAME: return objectValue(); case NONEMPTY_OBJECT: return nextInObject(false); case NONEMPTY_DOCUMENT: int c = nextNonWhitespace(false); if (c == -1) { return JsonToken.END_DOCUMENT; } pos--; if (!lenient) { throw syntaxError("Expected EOF"); } return nextValue(); case CLOSED: throw new IllegalStateException("JsonReader is closed"); default: throw new AssertionError(); }}
如下的代码是一个出口,分析的结果会保存到members中
public void add(String property, JsonElement value) { if (value == null) { value = JsonNull.INSTANCE; } members.put($Gson$Preconditions.checkNotNull(property), value);}当前的问题是peek和read是如何串起来的呢?
:在read的过程中,peek会一直调用,对内容的标志进一步分析,
待续!!
0 0
- Gson源码的研读-2015/10/9
- Gson 源码解析-2015/10/13
- Bundler源码研读笔记
- Webx源码研读心得
- java源码研读快捷键
- EventBus源码研读(上)
- EventBus源码研读(中)
- EventBus源码研读(上)
- EventBus源码研读(中)
- EventBus源码研读(下)
- EventBus源码研读(上)
- EventBus源码研读(上)
- EventBus源码研读(中)
- EventBus源码研读(下)
- EventBus源码研读
- iOS MJRefresh源码研读
- Tomcat8.0.0源码研读
- Spring源码研读
- BS学习 一 搭建 struts
- 通知中心
- readn writen readline函数源代码
- GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助
- Codeforces 584E. Anton and Ira (排列好题)
- Gson源码的研读-2015/10/9
- 起个名字好难:关于变量命名的思考与总结
- 归并排序
- android studio的一些编译错误
- Selenium+PhantomJS 爬取页面
- POJ 2965 The Pilots Brothers' refrigerator 枚举dfs
- lintcode-逆波兰表达式求值-424
- qt获取当前时间以及日期
- POJ 1014 && HDU 1059 Dividing(完全背包问题)