Package com.alibaba.fastjson

Examples of com.alibaba.fastjson.JSONReader


        reader.endArray();
        reader.close();
    }
   
    public void test_read_3() throws Exception {
        JSONReader reader = new JSONReader(new JSONScanner(text));
        reader.startArray();

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

        reader.endArray();
        reader.close();
    }
View Full Code Here


    String text = "{\"f0\":\"0\",\"f1\":\"1\",\"f2\":\"2\",\"f3\":\"3\",\"f4\":\"4\", " + //
                  "\"f5\":\"5\",\"f6\":\"6\",\"f7\":\"7\",\"f8\":\"8\",\"f9\":\"9\"}";

    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();
            String value = reader.readString();
            count++;
        }
        Assert.assertEquals(10, count);

        reader.endObject();
        reader.close();
    }
View Full Code Here

        reader.endObject();
        reader.close();
    }

    public void test_read_1() throws Exception {
        JSONReader reader = new JSONReader(new JSONScanner(text));

        reader.startObject();

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

        reader.endObject();
        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;

public class JSONReader_obj extends TestCase {

    public void test_array() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));

        reader.startArray();

        VO vo = new VO();
        reader.readObject(vo);

        Assert.assertEquals(123, vo.getId());

        reader.endArray();

        reader.close();
    }
View Full Code Here

        reader.close();
    }

    public void test_obj() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("{\"id\":123}"));

        VO vo = new VO();
        reader.readObject(vo);

        Assert.assertEquals(123, vo.getId());

        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;

public class JSONReader_string_1 extends TestCase {
    public void test_obj() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("\"abc\""));

        Assert.assertEquals("abc", reader.readString());

        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;

public class JSONReader_array extends TestCase {

    public void test_array() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[[],[],3,null,{\"name\":\"jobs\"},{\"id\":123},{\"id\":1},{\"id\":2}]"));
        reader.startArray();

        JSONArray first = (JSONArray) reader.readObject();
        JSONArray second = (JSONArray) reader.readObject();

        Assert.assertNotNull(first);
        Assert.assertNotNull(second);

        Assert.assertEquals(new Integer(3), reader.readInteger());
        Assert.assertNull(reader.readString());
       
        {
            Map<String, Object> map = new HashMap<String, Object>();
            reader.readObject(map);
            Assert.assertEquals("jobs", map.get("name"));
        }

        {
            VO vo = new VO();
            reader.readObject(vo);
            Assert.assertEquals(123, vo.getId());
        }
       
        while (reader.hasNext()) {
            VO vo = reader.readObject(VO.class);
            Assert.assertNotNull(vo);
        }
        reader.endArray();
        reader.close();
    }
View Full Code Here

public class JSONReaderTest extends TestCase {
  public void test_read() throws Exception {
    String resource = "2.json";
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        
    JSONReader reader = new JSONReader(new InputStreamReader(is, "UTF-8"));
   
    reader.startObject();
   
    Assert.assertEquals("company", reader.readString());
    Assert.assertTrue(reader.readObject() instanceof JSONObject);
   
    Assert.assertEquals("count", reader.readString());
    Assert.assertEquals(5, reader.readObject());
   
    Assert.assertEquals("pagecount", reader.readString());
    Assert.assertEquals(0, reader.readObject());
   
    Assert.assertEquals("pageindex", reader.readString());
    Assert.assertEquals(0, reader.readObject());
   
    Assert.assertEquals("resultList", reader.readString());
    Assert.assertTrue(reader.readObject() instanceof JSONArray);
   
    Assert.assertEquals("totalCount", reader.readString());
    Assert.assertEquals(0, reader.readObject());
   
    reader.endObject();
   
    reader.close();
  }
View Full Code Here

    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);
        }
        reader.close();
    }
View Full Code Here

import com.alibaba.fastjson.JSONReader;
import com.alibaba.fastjson.TypeReference;

public class JSONReader_typeRef extends TestCase {
    public void test_array() throws Exception {
        JSONReader reader = new JSONReader(new StringReader("[{\"id\":123}]"));
       
        List<VO> list = reader.readObject(new TypeReference<List<VO>>() {}.getType());
       
        Assert.assertEquals(123, list.get(0).getId());
       
        reader.close();
    }
View Full Code Here

TOP

Related Classes of com.alibaba.fastjson.JSONReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.