`
knight_black_bob
  • 浏览: 845247 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

json core

    博客分类:
  • core
阅读更多

 

 Json 源码

     The Software shall be used for Good, not Evil.

 

 

 

public class JSONTokener {

    private int 	character;
	private boolean eof;
    private int 	index;
    private int 	line;
    private char 	previous;
    private Reader 	reader;
    private boolean usePrevious;


    /**
     * Construct a JSONTokener from a Reader.
     *
     * @param reader     A reader.
     */
    public JSONTokener(Reader reader) {
        this.reader = reader.markSupported() ? 
        		reader : new BufferedReader(reader);
        this.eof = false;
        this.usePrevious = false;
        this.previous = 0;
        this.index = 0;
        this.character = 1;
        this.line = 1;
    }

  

 

   public Object nextValue() throws JSONException {
        char c = nextClean();
        String string;

        switch (c) {
            case '"':
            case '\'':
                return nextString(c);
            case '{':
                back();
                return new JSONObject(this);
            case '[':
                back();
                return new JSONArray(this);
        }

 

  public char nextClean() throws JSONException {
        for (;;) {
            char c = next();
            if (c == 0 || c > ' ') {
                return c;
            }
        }
    }

  

  public void back() throws JSONException {
        if (usePrevious || index <= 0) {
            throw new JSONException("Stepping back two steps is not supported");
        }
        this.index -= 1;
        this.character -= 1;
        this.usePrevious = true;
        this.eof = false;
    }

 

 public String nextString(char quote) throws JSONException {
        char c;
        StringBuffer sb = new StringBuffer();
        for (;;) {
            c = next();
            switch (c) {
            case 0:
            case '\n':
            case '\r':
                throw syntaxError("Unterminated string");
            case '\\':
                c = next();
                switch (c) {
                case 'b':
                    sb.append('\b');
                    break;
                case 't':
                    sb.append('\t');
                    break;
                case 'n':
                    sb.append('\n');
                    break;
                case 'f':
                    sb.append('\f');
                    break;
                case 'r':
                    sb.append('\r');
                    break;
                case 'u':
                    sb.append((char)Integer.parseInt(next(4), 16));
                    break;
                case '"':
                case '\'':
                case '\\':
                case '/':
                	sb.append(c);
                	break;
                default:
                    throw syntaxError("Illegal escape.");
                }
                break;
            default:
                if (c == quote) {
                    return sb.toString();
                }
                sb.append(c);
            }
        }
    }

 

 

 

 

 public JSONObject(JSONTokener x) throws JSONException {
        this();
        char c;
        String key;

        if (x.nextClean() != '{') {
            throw x.syntaxError("A JSONObject text must begin with '{'");
        }
        for (;;) {
            c = x.nextClean();
            switch (c) {
            case 0:
                throw x.syntaxError("A JSONObject text must end with '}'");
            case '}':
                return;
            default:
                x.back();
                key = x.nextValue().toString();
            }

// The key is followed by ':'. We will also tolerate '=' or '=>'.

            c = x.nextClean();
            if (c == '=') {
                if (x.next() != '>') {
                    x.back();
                }
            } else if (c != ':') {
                throw x.syntaxError("Expected a ':' after a key");
            }
            putOnce(key, x.nextValue());

// Pairs are separated by ','. We will also tolerate ';'.

            switch (x.nextClean()) {
            case ';':
            case ',':
                if (x.nextClean() == '}') {
                    return;
                }
                x.back();
                break;
            case '}':
                return;
            default:
                throw x.syntaxError("Expected a ',' or '}'");
            }
        }
    }

 

 

  public JSONObject putOnce(String key, Object value) throws JSONException {
        if (key != null && value != null) {
            if (opt(key) != null) {
                throw new JSONException("Duplicate key \"" + key + "\"");
            }
            put(key, value);
        }
        return this;
    }

 

   @SuppressWarnings("unchecked")
	public JSONObject put(String key, Object value) throws JSONException {
        if (key == null) {
            throw new JSONException("Null key.");
        }
        if (value != null) {
            testValidity(value);
            this.map.put(key, value);
        } else {
            remove(key);
        }
        return this;
    }

  

 

public JSONArray(JSONTokener x) throws JSONException {
        this();
        if (x.nextClean() != '[') {
            throw x.syntaxError("A JSONArray text must start with '['");
        }
        if (x.nextClean() != ']') {
	        x.back();
	        for (;;) {
	            if (x.nextClean() == ',') {
	                x.back();
	                this.myArrayList.add(JSONObject.NULL);
	            } else {
	                x.back();
	                this.myArrayList.add(x.nextValue());
	            }
	            switch (x.nextClean()) {
	            case ';':
	            case ',':
	                if (x.nextClean() == ']') {
	                    return;
	                }
	                x.back();
	                break;
	            case ']':
	            	return;
	            default:
	                throw x.syntaxError("Expected a ',' or ']'");
	            }
	        }
        }
    }

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

分享到:
评论

相关推荐

    rapidjson解析json代码实例以及常见的json core dump问题

    rapidjson解析json代码实例 直接看代码: #include #include #include #include #include #include #include // 请自己下载开源的rapidjson #include "rapidjson/prettywriter.h" #include "rapidjson/...

    json4s-core_2.11-3.2.11-API文档-中文版.zip

    赠送jar包:json4s-core_2.11-3.2.11.jar; 赠送原API文档:json4s-core_2.11-3.2.11-javadoc.jar; 赠送源代码:json4s-core_2.11-3.2.11-sources.jar; 赠送Maven依赖信息文件:json4s-core_2.11-3.2.11.pom; ...

    json-unit-core-1.1.3.zip

    JSON Unit Core 1.1.3 是一个专门用于测试JSON数据的开源项目,它为Java开发者提供了一套强大的工具来验证JSON对象是否符合预期。在软件开发中,特别是在API测试和Web服务验证方面,JSON Unit Core是不可或缺的一...

    jackson2.6 jar包(3个包)

    1. **Jackson核心库** (如:`jackson-core-2.6.x.jar`):这是Jackson框架的基础,提供了基本的JSON解析和生成功能。包括流式API(JsonParser和JsonGenerator),用于低级的读写操作,以及JsonNode对象模型,可以对...

    springmvc加入json支持

    2. `jackson-core-2.4.2.jar`: Jackson核心库提供了基本的JSON解析和生成功能。它是其他Jackson模块(如databind和annotations)的基础,包含了流式API(JsonParser和JsonGenerator)以及数据绑定的基础结构。 3. `...

    asp.net Core Post Json方式给控制器Action传参并拦截丢失参数等错误信息

    原生asp.net不支持通过json提交 方式给控制器的Action传参数,本程序通过给自定义输入字符串格式方法。在action的传参上加入FromJsonKey属性即可在传入的json中传入参数,如果传入的json中不存在相应键即报错 ...

    json-schema-core-1.2.1.jar

    json-schema-core-1.2.1.jar

    NewtonJson_NewtonJson_JSON_json解析_presentd78_

    Newtonsoft.Json是一个由James Newton-King开发的.NET库,也称为Json.NET,它为.NET Framework、.NET Core、Unity等平台提供了全面且高性能的JSON处理能力。标题中的"NewtonJson_NewtonJson_JSON_json解析_presentd...

    json所需要的夹包,json解析用,json,json夹包和教程,解析json所需要的夹包

    Jackson库包括多个模块,如`Jackson-databind`用于映射JSON到Java对象,`Jackson-core`提供基本的处理功能,而`Jackson-annotations`则支持注解驱动的配置。 2. **Gson** - 由Google开发,它可以直接将Java对象转换...

    json必须的jar文件,用json传输的来看看

    `jackson-core`提供了基本的JSON流处理API,`jackson-databind`则提供了将Java对象映射到JSON和反向映射的功能,而`jackson-annotations`包含了一些常用的注解,用于自定义序列化和反序列化过程。 Gson是Google提供...

    Java bean转换为Json Schema

    import com.github.fge.jsonschema.core.exceptions.ProcessingException; import com.github.fge.jsonschema.core.report.ProcessingMessage; import com.github.fge.jsonschema.util.JsonLoader; import org.json...

    json完整版下载

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成。它基于JavaScript的一个子集,但被广泛应用于各种编程语言中,包括Java。在Java中,SSH(Spring、...

    C#读取修改Json文件

    首先,C#的标准库.NET Framework并没有内置对JSON的支持,但.NET Core和.NET 5及以上版本引入了System.Text.Json,它是官方推荐的JSON处理库。然而,对于更丰富的功能和易用性,许多开发者选择使用第三方库如...

    JSON Schema 校验库——json-schema-validator(java版本).rar

    import com.github.fge.jsonschema.core.exceptions.ProcessingException; import com.github.fge.jsonschema.main.JsonSchema; import com.github.fge.jsonschema.main.JsonSchemaFactory; import ...

    Java中使用Json

    Jackson是Java中常用的JSON处理库,它提供了多个模块,如core、bind和dataformat。其中,`ObjectMapper`类是Jackson的核心,它可以实现JSON和Java对象间的映射。例如,我们可以使用以下代码将一个Java对象转换为...

    Json包及API Json工具包

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,被广泛用于Web服务和应用程序之间的数据传输。它易于人阅读和编写,同时也易于机器解析和生成。JSON包及API是编程语言中的工具集,用于处理JSON格式...

    json所需要的jar

    1. Jackson:Jackson是一个高性能、功能丰富的库,它包括了用于JSON序列化和反序列化的多个模块,如`jackson-databind`、`jackson-core`和`jackson-annotations`。Jackson支持多种映射策略,可以方便地将Java对象...

    C#.net 使用json作为配置文件,解悉JSON示例

    此外,如果你的项目使用了ASP.NET Core,可以利用`Microsoft.Extensions.Configuration.Json`包,它提供了一种更高级的方式来管理和加载JSON配置。首先安装该包: ```bash dotnet add package Microsoft.Extensions...

Global site tag (gtag.js) - Google Analytics