Package org.codehaus.jackson

Examples of org.codehaus.jackson.JsonParser


    {
        StringWriter sw = new StringWriter();
        json.writeTo(sw, foo);
        String generated = sw.toString();

        JsonParser parser = json.getJsonFactory().createJsonParser(generated);
        Foo parsedFoo = json.parseFrom(parser, Foo.class);
        parser.close();
       
        assertEquals(foo, parsedFoo);       
       
        return generated;
    }
View Full Code Here


    {
        StringWriter sw = new StringWriter();
        json.writeTo(sw, fooList, Foo.class);
        String generated = sw.toString();
       
        JsonParser parser = json.getJsonFactory().createJsonParser(generated);
        json.appendMessageFrom(parser, parsedFooList, Foo.class);
        parser.close();
        assertTrue(fooList.size() == parsedFooList.size());
        for(int i=0,len=fooList.size(); i<len; i++)
            assertEquals(fooList.get(i), parsedFooList.get(i));

        return generated;
View Full Code Here

    ZNRecord record = null;

    try
    {
      JsonFactory f = new JsonFactory();
      JsonParser jp = f.createJsonParser(bais);

      jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
      while (jp.nextToken() != JsonToken.END_OBJECT)
      {
        String fieldname = jp.getCurrentName();
        jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
        if ("id".equals(fieldname))
        {
          // contains an object
          record = new ZNRecord(jp.getText());
        }
        else if ("simpleFields".equals(fieldname))
        {
          while (jp.nextToken() != JsonToken.END_OBJECT)
          {
            String key = jp.getCurrentName();
            jp.nextToken(); // move to value
            record.setSimpleField(key, jp.getText());
          }
        }
        else if ("mapFields".equals(fieldname))
        {
          // user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE);
          while (jp.nextToken() != JsonToken.END_OBJECT)
          {
            String key = jp.getCurrentName();
            record.setMapField(key, new TreeMap<String, String>());
            jp.nextToken(); // move to value

            while (jp.nextToken() != JsonToken.END_OBJECT)
            {
              String mapKey = jp.getCurrentName();
              jp.nextToken(); // move to value
              record.getMapField(key).put(mapKey, jp.getText());
            }
          }

        }
        else if ("listFields".equals(fieldname))
        {
          // user.setUserImage(jp.getBinaryValue());
          while (jp.nextToken() != JsonToken.END_OBJECT)
          {
            String key = jp.getCurrentName();
            record.setListField(key, new ArrayList<String>());
            jp.nextToken(); // move to value
            while (jp.nextToken() != JsonToken.END_ARRAY)
            {
              record.getListField(key).add(jp.getText());
            }

          }

        }
        else
        {
          throw new IllegalStateException("Unrecognized field '" + fieldname + "'!");
        }
      }
      jp.close(); // ensure resources get cleaned up timely and properly
    }
    catch (Exception e)
    {
      LOG.error("Exception during deserialization of bytes: " + new String(bytes), e);
    }
View Full Code Here

    public static int importSorted(String jsonFile, ColumnFamily columnFamily, String ssTablePath, IPartitioner<?> partitioner) throws IOException
    {
        int importedKeys = 0; // already imported keys count
        long start = System.currentTimeMillis();

        JsonParser parser = getParser(jsonFile);

        if (keyCountToImport == null)
        {
            keyCountToImport = 0;
            System.out.println("Counting keys to import, please wait... (NOTE: to skip this use -n <num_keys>)");

            parser.nextToken(); // START_OBJECT
            while (parser.nextToken() != null)
            {
                parser.nextToken();
                parser.skipChildren();
                if (parser.getCurrentName() == null) continue;

                keyCountToImport++;
            }
        }

        System.out.printf("Importing %s keys...%n", keyCountToImport);

        parser = getParser(jsonFile); // renewing parser
        SSTableWriter writer = new SSTableWriter(ssTablePath, keyCountToImport);

        int lineNumber = 1;
        DecoratedKey prevStoredKey = null;

        while (parser.nextToken() != null)
        {
            String key = parser.getCurrentName();

            if (key != null)
            {
                String tokenName = parser.nextToken().name();

                if (tokenName.equals("START_ARRAY"))
                {
                    if (columnFamily.getType() == ColumnFamilyType.Super)
                    {
                        throw new RuntimeException("Can't write Standard columns to the Super Column Family.");
                    }

                    List<?> columns = parser.readValueAs(new TypeReference<List<?>>() {});
                    addToStandardCF(columns, columnFamily);
                }
                else if (tokenName.equals("START_OBJECT"))
                {
                    if (columnFamily.getType() == ColumnFamilyType.Standard)
                    {
                        throw new RuntimeException("Can't write Super columns to the Standard Column Family.");
                    }

                    Map<?, ?> columns = parser.readValueAs(new TypeReference<Map<?, ?>>() {});
                    addToSuperCF(columns, columnFamily);
                }
                else
                {
                    throw new UnsupportedOperationException("Only Array or Hash allowed as row content.");
View Full Code Here

            if (assumptionFile.isFile())
            {
                try
                {
                    JsonFactory f = new JsonFactory();
                    JsonParser p = f.createJsonParser(assumptionFile);
                    JsonToken token = p.nextToken();
                    while (token != JsonToken.END_OBJECT)
                    {
                        if (token == JsonToken.FIELD_NAME)
                        {
                            String keyspace = p.getText();
                            Map<String, Map<String, String>> ksAssumes = assumptions.get(keyspace);
                            if (ksAssumes == null)
                            {
                                ksAssumes = new HashMap<String, Map<String, String>>();
                                assumptions.put(keyspace,  ksAssumes);
                            }
                            token = p.nextToken();
                            while (token != JsonToken.END_ARRAY)
                            {
                                if (token == JsonToken.FIELD_NAME)
                                {
                                    String columnFamily = p.getText();
                                    Map<String, String> cfAssumes = ksAssumes.get(columnFamily);
                                    if (cfAssumes == null)
                                    {
                                        cfAssumes = new HashMap<String, String>();
                                        ksAssumes.put(columnFamily, cfAssumes);
                                    }
                                   
                                    token = p.nextToken();
                                    while (token != JsonToken.END_ARRAY)
                                    {
                                        if (token == JsonToken.FIELD_NAME)
                                        {
                                            String prop = p.getText();
                                            p.nextToken();
                                            String value = p.getText();
                                            cfAssumes.put(prop, value);
                                        }
                                       
                                        token = p.nextToken();
                                    }
                                }
                                token = p.nextToken();
                            }
                        }
                        token = p.nextToken();
                    }
                    sessionState.out.println("Column Family assumptions read from " + assumptionFile);
                }
                catch (Exception e)
                {
View Full Code Here

   */
  @Override
  public Object deserialize(Writable blob) throws SerDeException {

    Text t = (Text) blob;
    JsonParser p;
    List<Object> r = new ArrayList<Object>(Collections.nCopies(columnNames.size(), null));
    try {
      p = jsonFactory.createJsonParser(new ByteArrayInputStream((t.getBytes())));
      if (p.nextToken() != JsonToken.START_OBJECT) {
        throw new IOException("Start token not found where expected");
      }
      JsonToken token;
      while (((token = p.nextToken()) != JsonToken.END_OBJECT) && (token != null)) {
        // iterate through each token, and create appropriate object here.
        populateRecord(r, token, p, schema);
      }
    } catch (JsonParseException e) {
      LOG.warn("Error [{}] parsing json text [{}].", e, t);
View Full Code Here

    public DirectMemoryRequest buildRequest( InputStream inputStream )
        throws DirectMemoryException
    {
        try
        {
            JsonParser jp = this.jsonFactory.createJsonParser( inputStream );
            DirectMemoryRequest rq = new DirectMemoryRequest();
            JsonToken jsonToken = jp.nextToken();
            while ( jsonToken != JsonToken.END_OBJECT && jsonToken != null )
            {
                String fieldName = jp.getCurrentName();
                if ( DirectMemoryConstants.KEY_FIELD_NAME.equals( fieldName ) )
                {
                    rq.setKey( jp.getText() );
                }
                if ( DirectMemoryConstants.PUT_FIELD_NAME.equals( fieldName ) )
                {
                    rq.setUpdate( jp.getValueAsBoolean() );
                }
                if ( DirectMemoryConstants.EXPIRES_IN_FIELD_NAME.equals( fieldName ) )
                {
                    rq.setExpiresIn( jp.getValueAsInt() );
                }
                if ( DirectMemoryConstants.CACHE_CONTENT_FIELD_NAME.equals( fieldName ) )
                {
                    // binaryValue need to go to nextToken
                    jp.nextToken();
                    rq.setCacheContent( jp.getBinaryValue() );
                }
                jsonToken = jp.nextToken();
            }

            jp.close();

            return rq;
        }
        catch ( JsonParseException e )
        {
View Full Code Here

    public DirectMemoryResponse buildResponse( InputStream inputStream )
        throws DirectMemoryException
    {
        try
        {
            JsonParser jp = this.jsonFactory.createJsonParser( inputStream );
            DirectMemoryResponse rs = new DirectMemoryResponse();

            JsonToken jsonToken = jp.nextToken();

            while ( jsonToken != JsonToken.END_OBJECT && jsonToken != null)
            {
                String fieldName = jp.getCurrentName();
                if ( DirectMemoryConstants.FOUND_FIELD_NAME.equals( fieldName ) )
                {
                    rs.setFound( jp.getValueAsBoolean() );
                }
                if ( DirectMemoryConstants.KEY_FIELD_NAME.equals( fieldName ) )
                {
                    rs.setKey( jp.getText() );
                }
                if ( DirectMemoryConstants.CACHE_CONTENT_FIELD_NAME.equals( fieldName ) )
                {
                    // binaryValue need to go to nextToken
                    jp.nextToken();
                    rs.setCacheContent( jp.getBinaryValue() );
                }
                jsonToken = jp.nextToken();
            }


            return rs;
        }
View Full Code Here

    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ZNRecord record = null;

    try {
      JsonFactory f = new JsonFactory();
      JsonParser jp = f.createJsonParser(bais);

      jp.nextToken(); // will return JsonToken.START_OBJECT (verify?)
      while (jp.nextToken() != JsonToken.END_OBJECT) {
        String fieldname = jp.getCurrentName();
        jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY
        if ("id".equals(fieldname)) {
          // contains an object
          record = new ZNRecord(jp.getText());
        } else if ("simpleFields".equals(fieldname)) {
          while (jp.nextToken() != JsonToken.END_OBJECT) {
            String key = jp.getCurrentName();
            jp.nextToken(); // move to value
            record.setSimpleField(key, jp.getText());
          }
        } else if ("mapFields".equals(fieldname)) {
          // user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE);
          while (jp.nextToken() != JsonToken.END_OBJECT) {
            String key = jp.getCurrentName();
            record.setMapField(key, new TreeMap<String, String>());
            jp.nextToken(); // move to value

            while (jp.nextToken() != JsonToken.END_OBJECT) {
              String mapKey = jp.getCurrentName();
              jp.nextToken(); // move to value
              record.getMapField(key).put(mapKey, jp.getText());
            }
          }

        } else if ("listFields".equals(fieldname)) {
          // user.setUserImage(jp.getBinaryValue());
          while (jp.nextToken() != JsonToken.END_OBJECT) {
            String key = jp.getCurrentName();
            record.setListField(key, new ArrayList<String>());
            jp.nextToken(); // move to value
            while (jp.nextToken() != JsonToken.END_ARRAY) {
              record.getListField(key).add(jp.getText());
            }

          }

        } else if ("rawPayload".equals(fieldname)) {
          record.setRawPayload(Base64.decode(jp.getText()));
        } else {
          throw new IllegalStateException("Unrecognized field '" + fieldname + "'!");
        }
      }
      jp.close(); // ensure resources get cleaned up timely and properly
    } catch (Exception e) {
      LOG.error("Exception during deserialization of bytes: " + new String(bytes), e);
    }

    return record;
View Full Code Here

  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

  @Override
  public Trace decode(InputStream inputStream) throws IOException
  {
    final JsonParser parser = OBJECT_MAPPER.getJsonFactory().createJsonParser(inputStream);
    final JsonNode rootNode = OBJECT_MAPPER.readTree(parser);
    return JsonTraceDeserializer.deserialize(rootNode);
  }
View Full Code Here

TOP

Related Classes of org.codehaus.jackson.JsonParser

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.