Java 类com.alibaba.fastjson.JSONReader 实例源码

项目:GitHub    文件:DateFieldTest11_reader.java   
public void test_cn() throws Exception {
    Model vo = new JSONReader(new StringReader("{\"date0\":\"2016-05-06\",\"date1\":\"2017-03-01\"}")).readObject(Model.class);

    Calendar calendar = Calendar.getInstance(JSON.defaultTimeZone, JSON.defaultLocale);

    calendar.setTime(vo.date0);
    assertEquals(2016, calendar.get(Calendar.YEAR));
    assertEquals(4, calendar.get(Calendar.MONTH));
    assertEquals(6, calendar.get(Calendar.DAY_OF_MONTH));
    assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(0, calendar.get(Calendar.MINUTE));
    assertEquals(0, calendar.get(Calendar.SECOND));
    assertEquals(0, calendar.get(Calendar.MILLISECOND));

    calendar.setTime(vo.date1);
    assertEquals(2017, calendar.get(Calendar.YEAR));
    assertEquals(2, calendar.get(Calendar.MONTH));
    assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
    assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
    assertEquals(0, calendar.get(Calendar.MINUTE));
    assertEquals(0, calendar.get(Calendar.SECOND));
    assertEquals(0, calendar.get(Calendar.MILLISECOND));
}
项目:GitHub    文件:JSONReaderTest_object_object.java   
public void test_read() throws Exception {

        JSONReader reader = new JSONReader(new StringReader(text));
        reader.startObject();

        int count = 0;
        while (reader.hasNext()) {
            String key = (String) reader.readObject();
            Object value = reader.readObject();
            Assert.assertNotNull(key);
            Assert.assertNotNull(value);
            count++;
        }
        Assert.assertEquals(10, count);

        reader.endObject();
        reader.close();
    }
项目:GitHub    文件:JSONReaderTest_object_int.java   
public void test_read() throws Exception {
    String text = "{\"f0\":0,\"f1\":1,\"f2\":2,\"f3\":3,\"f4\":4, " + //
                  "\"f5\":5,\"f6\":6,\"f7\":7,\"f8\":8,\"f9\":9}";
    JSONReader reader = new JSONReader(new StringReader(text));
    reader.startObject();

    int count = 0;
    while (reader.hasNext()) {
        String key = (String) reader.readObject();
        Integer value = reader.readInteger();
        count++;
    }
    Assert.assertEquals(10, count);

    reader.endObject();
    reader.close();
}
项目:GitHub    文件:JSONReaderScannerTest_error4.java   
public void test_e() throws Exception {
    Exception error = null;
    try {
        StringBuilder buf = new StringBuilder();
        buf.append("[{\"type\":\"");
        for (int i = 0; i < 8180; ++i) {
            buf.append('A');
        }
        buf.append("\\t");
        JSONReader reader = new JSONReader(new MyReader(buf.toString()));
        reader.readObject();
        reader.close();
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:CustomDeserializerTest.java   
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    JSONReader reader = new JSONReader(parser);

    reader.startObject();

    String key = reader.readString();
    Integer value = reader.readInteger();

    Model model = new Model();
    model.id = value;

    reader.endObject();
    // TODO Auto-generated method stub
    return (T) model;
}
项目:GitHub    文件:LongTest2_obj.java   
public void test_1() throws Exception {
    String json = "{\"v1\":-1883391953414482124,\"v2\":-3019416596934963650,\"v3\":6497525620823745793,\"v4\":2136224289077142499,\"v5\":-2090575024006307745}";
    String json2 = "{\"v1\":\"-1883391953414482124\",\"v2\":\"-3019416596934963650\",\"v3\":\"6497525620823745793\",\"v4\":\"2136224289077142499\",\"v5\":\"-2090575024006307745\"}";

    Model m1 = new JSONReader(new StringReader(json)).readObject(Model.class);
    Model m2 = new JSONReader(new StringReader(json2)).readObject(Model.class);

    assertNotNull(m1);
    assertNotNull(m2);

    assertEquals(-1883391953414482124L, m1.v1.longValue());
    assertEquals(-3019416596934963650L, m1.v2.longValue());
    assertEquals(6497525620823745793L, m1.v3.longValue());
    assertEquals(2136224289077142499L, m1.v4.longValue());
    assertEquals(-2090575024006307745L, m1.v5.longValue());

    assertEquals(-1883391953414482124L, m2.v1.longValue());
    assertEquals(-3019416596934963650L, m2.v2.longValue());
    assertEquals(6497525620823745793L, m2.v3.longValue());
    assertEquals(2136224289077142499L, m2.v4.longValue());
    assertEquals(-2090575024006307745L, m2.v5.longValue());
}
项目:GitHub    文件:JSONReader_obj_3.java   
public void test_obj_3() throws Exception {
    JSONReader reader = new JSONReader(new StringReader("{\"val\":{\"val\":{\"id\":123}}}"));

    reader.startObject();
    Assert.assertEquals("val", reader.readString());

    reader.startObject();
    Assert.assertEquals("val", reader.readString());

    reader.startObject();
    Assert.assertEquals("id", reader.readString());
    Assert.assertEquals(Long.valueOf(123), reader.readLong());
    reader.endObject();

    reader.endObject();
    reader.endObject();

    reader.close();
}
项目:GitHub    文件:JSONReaderTest_error2.java   
public void test_read() throws Exception {
    Field field = JSONReader.class.getDeclaredField("context");
    field.setAccessible(true);
    ;

    JSONReader reader = new JSONReader(new StringReader("[{}]"));
    reader.config(Feature.AllowArbitraryCommas, true);

    reader.startArray();

    context = field.get(reader);
    stateField = context.getClass().getDeclaredField("state");
    stateField.setAccessible(true);


    {
        Exception error = null;
        try {
            reader.readObject(VO.class);
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
}
项目:GitHub    文件:JSONReaderTest_array_object_2.java   
public void test_read() throws Exception {

        JSONReader reader = new JSONReader(new StringReader(text));
        reader.startArray();

        int count = 0;
        while (reader.hasNext()) {
            reader.startObject();
            reader.endObject();
            count++;
        }
        Assert.assertEquals(10, count);

        reader.endArray();
        reader.close();
    }
项目:GitHub    文件:JSONReaderTest_array_array.java   
public void test_read() throws Exception {

        JSONReader reader = new JSONReader(new StringReader(text));
        reader.startArray();

        int count = 0;
        while (reader.hasNext()) {
            Object item = reader.readObject();
            Assert.assertEquals(JSONArray.class, item.getClass());
            count++;
        }
        Assert.assertEquals(10, count);

        reader.endArray();
        reader.close();
    }
项目:GitHub    文件:JSONReaderScannerTest_error5.java   
public void test_e() throws Exception {
    Exception error = null;
    try {
        StringBuilder buf = new StringBuilder();
        buf.append("[{\"type\":\"");
        for (int i = 0; i < 8180; ++i) {
            buf.append('A');
        }
        buf.append("\\t");
        JSONReader reader = new JSONReader(new MyReader(buf.toString()));
        reader.readObject();
        reader.close();
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:ListStringFieldTest_stream.java   
public void test_list() throws Exception {
    String text = "{\"values\":[\"a\",null,\"b\",\"ab\\\\c\\\"\"]}";

    JSONReader reader = new JSONReader(new StringReader(text));
    Model model = reader.readObject(Model.class);
    Assert.assertEquals(4, model.values.size());
    Assert.assertEquals("a", model.values.get(0));
    Assert.assertEquals(null, model.values.get(1));
    Assert.assertEquals("b", model.values.get(2));
    Assert.assertEquals("ab\\c\"", model.values.get(3));
}
项目:GitHub    文件:ReaderIntFieldTest.java   
public void test_int_error_end() throws Exception {
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader("{\"value\":1001,\"value2\":-2001["));
        reader.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:StringFieldTest_special.java   
public void test_special_2() throws Exception {
    Model model = new Model();
    model.name = "a\\bc\"";
    String text = JSON.toJSONString(model);
    Assert.assertEquals("{\"name\":\"a\\\\bc\\\"\"}", text);

    JSONReader reader = new JSONReader(new StringReader(text));
    Model model2 = reader.readObject(Model.class);
    Assert.assertEquals(model.name, model2.name);
    reader.close();
}
项目:GitHub    文件:ListStringFieldTest_stream_array.java   
public void test_error_null() throws Exception {
    String text = "[null";
    JSONReader reader = new JSONReader(new StringReader(text));

    Exception error = null;
    try {
        reader.readObject(Model.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONReaderScannerTest_error.java   
public void test_e() throws Exception {
    Exception error = null;
    try {
        new JSONReader(new MyReader());
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_long_public.java   
public void test_error_overflow_stream() throws Exception {
    String text = "[2147483649:\"wenshao\" ]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_long_public.java   
public void test_error_value_notmatch_stream() throws Exception {
    String text = "[true,\"wenshao\"]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_long_public.java   
public void test_error_value_notmatch_2_stream() throws Exception {
    String text = "[+,\"wenshao\"]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:ListStringFieldTest_stream.java   
public void test_error_null() throws Exception {
    String text = "{\"values\":[null";
    JSONReader reader = new JSONReader(new StringReader(text));

    Exception error = null;
    try {
        reader.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:LongNullTest.java   
public void test_null_array_reader() throws Exception {
    JSONReader reader = new JSONReader(new StringReader("[\"null\" ,\"null\"]"), Feature.SupportArrayToBean);
    Model model = reader.readObject(Model.class);
    assertNotNull(model);
    assertNull(model.v1);
    assertNull(model.v2);
}
项目:GitHub    文件:WriteAsArray_int_public.java   
public void test_error_stream() throws Exception {
    String text = "[123.,\"wenshao\" ]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_int_public.java   
public void test_error_stream_1() throws Exception {
    String text = "[123:\"wenshao\" ]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:EnumFieldTest2.java   
public void test_error_1() throws Exception {
    Exception error = null;
    try {
        JSONReader read = new JSONReader(new StringReader("[null:null]"));
        read.config(Feature.SupportArrayToBean, true);
        Model model = read.readObject(Model.class);
        read.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_int_public.java   
public void test_error_overflow_stream() throws Exception {
    String text = "[2147483649:\"wenshao\" ]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:ListStringFieldTest_stream_hashSet.java   
public void test_error_3() throws Exception {
    String text = "{\"model\":{\"values\":[]}[";
    JSONReader reader = new JSONReader(new StringReader(text));


    Exception error = null;
    try {
        reader.readObject(new TypeReference<Map<String, Model>>() {
        });
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_int_public.java   
public void test_error_value_notmatch_2_stream() throws Exception {
    String text = "[+,\"wenshao\"]";
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader(text), Feature.SupportArrayToBean);
        reader.readObject(VO.class);
        reader.close();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:WriteAsArray_string_special_Reader.java   
public void test_0() throws Exception {
    Model model = new Model();
    model.name = "a\\bc";
    String text = JSON.toJSONString(model, SerializerFeature.BeanToArray);
    Assert.assertEquals("[\"a\\\\bc\"]", text);

    JSONReader reader = new JSONReader(new StringReader(text));
    reader.config(Feature.SupportArrayToBean, true);
    Model model2 = reader.readObject(Model.class);
    Assert.assertEquals(model.name, model2.name);
    reader.close();
}
项目:GitHub    文件:WriteAsArray_string_special_Reader.java   
public void test_1() throws Exception {
    Model model = new Model();
    model.name = "a\\bc\"";
    String text = JSON.toJSONString(model, SerializerFeature.BeanToArray);
    Assert.assertEquals("[\"a\\\\bc\\\"\"]", text);

    JSONReader reader = new JSONReader(new StringReader(text));
    reader.config(Feature.SupportArrayToBean, true);
    Model model2 = reader.readObject(Model.class);
    Assert.assertEquals(model.name, model2.name);
    reader.close();
}
项目:GitHub    文件:ReaderIntFieldTest.java   
public void test_int_error_map() throws Exception {
    Exception error = null;
    try {
        JSONReader reader = new JSONReader(new StringReader("{\"model\":{\"value\":3001,\"value2\":-4001}["));
        reader.readObject(new TypeReference<Map<String, Model>>() {
        });
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:Issue1001.java   
public void test_for_issue() throws Exception {
    File file = new File("/Users/wenshao/Downloads/issue_1001.json");

    String json = FileUtils.readFileToString(file);

    JSONReader reader = new JSONReader(new StringReader(json));
    reader.readObject();
}
项目:GitHub    文件:IntegerNullTest.java   
public void test_null_array_reader() throws Exception {
    JSONReader reader = new JSONReader(new StringReader("[\"null\" ,\"null\"]"), Feature.SupportArrayToBean);
    Model model = reader.readObject(Model.class);
    assertNotNull(model);
    assertNull(model.v1);
    assertNull(model.v2);
}
项目:GitHub    文件:EnumFieldTest.java   
public void test_map() throws Exception {
    JSONReader read = new JSONReader(new StringReader("{\"model\":{\"value\":\"A\",\"value1\":\"B\"}}"));
    Map<String, Model> map = read.readObject(new TypeReference<Map<String, Model>>(){});
    Model model = (Model) map.get("model");
    Assert.assertEquals(Type.A, model.value);
    Assert.assertEquals(Type.B, model.value1);
    read.close();
}
项目:GitHub    文件:Issue1423.java   
public void test_for_issue_reader() throws Exception {
    Exception error = null;
    try {
        new JSONReader(new StringReader("{\"v\":9223372036854775808}")).readObject(LongVal.class);
    } catch (JSONException e) {
        error = e;
    }
    assertNotNull(error);
}
项目:GitHub    文件:Issue1423.java   
public void test_for_issue_arrayMapping_reader() throws Exception {
    Exception error = null;
    try {
        new JSONReader(new StringReader("[9223372036854775808]"), Feature.SupportArrayToBean).readObject(LongVal.class);
    } catch (JSONException e) {
        error = e;
    }
    assertNotNull(error);
    error.printStackTrace();
}
项目:GitHub    文件:BooleanFieldTest2.java   
public void test_1_reader() throws Exception {
    String text = "{\"value\":1}";

    JSONReader reader = new JSONReader(new StringReader(text));
    Model model = reader.readObject(Model.class);
    Assert.assertTrue(model.value);
    reader.close();
}
项目:GitHub    文件:BeanToArrayTest_date.java   
public void test_date2_reader() throws Exception {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", JSON.defaultLocale);
    dateFormat.setTimeZone(JSON.defaultTimeZone);

    Model model = new JSONReader(new StringReader("[\"2016-01-01\",\"2016-01-02\"]"), Feature.SupportArrayToBean).readObject(Model.class);
    Assert.assertEquals(dateFormat.parse("2016-01-01").getTime(), model.v1.getTime());
    Assert.assertEquals(dateFormat.parse("2016-01-02").getTime(), model.v2.getTime());
}
项目:GitHub    文件:EnumFieldTest2_private.java   
public void test_error_n() throws Exception {
    Exception error = null;
    try {
        JSONReader read = new JSONReader(new StringReader("[n"));
        read.config(Feature.SupportArrayToBean, true);
        Model model = read.readObject(Model.class);
        read.readObject(Model.class);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONReaderTest_1.java   
public void test_read() throws Exception {
    String text = "{\"id\":1001}";
    JSONReader reader = new JSONReader(new StringReader(text));
    Assert.assertEquals(JSONToken.LBRACE, reader.peek());
    reader.startObject();
    Assert.assertEquals(JSONToken.LITERAL_STRING, reader.peek());
    Assert.assertEquals("id", reader.readString());
    Assert.assertEquals(JSONToken.COLON, reader.peek());
    Assert.assertEquals(Integer.valueOf(1001), reader.readInteger());
    reader.endObject();

    reader.close();
}
项目:GitHub    文件:JSONReaderTest_3.java   
public void test_read_Long() throws Exception {
    String text = "1001";
    JSONReader reader = new JSONReader(new StringReader(text));

    Exception error = null;
    try {
        reader.hasNext();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}