List of usage examples for com.fasterxml.jackson.core JsonToken START_OBJECT
JsonToken START_OBJECT
To view the source code for com.fasterxml.jackson.core JsonToken START_OBJECT.
Click Source Link
From source file:com.cinnober.msgcodec.json.JsonCodec.java
/** * Read a static group from the specified stream, when the JSON does not contain the '$type' field. * * @param groupType the expected group type, not null. * @param in the stream to read from, not null. * @return the decoded value.// ww w.ja v a 2 s. co m * @throws IOException if the underlying stream throws an exception. * @throws DecodeException if the value could not be decoded, or if a required field is missing. */ public Object decodeStatic(Object groupType, InputStream in) throws IOException { StaticGroupHandler groupHandler = staticGroupsByGroupType.get(groupType); if (groupHandler == null) { throw new IllegalArgumentException("Unknown group type"); } JsonFactory f = new JsonFactory(); JsonParser p = f.createParser(in); JsonToken token = p.nextToken(); if (token == JsonToken.VALUE_NULL) { return null; } else if (token != JsonToken.START_OBJECT) { throw new DecodeException("Expected {"); } return groupHandler.readValue(p); }
From source file:org.apache.lucene.server.handlers.AddDocumentHandler.java
/** Parse a Document using Jackson's streaming parser * API. The document should look like {indexName: 'foo', * fields: {..., ...}} *///from w ww. j a v a 2s.c om public static Document parseDocument(IndexState state, JsonParser p) throws IOException { JsonToken token = p.nextToken(); if (token == JsonToken.END_ARRAY) { // nocommit hackish.. caller should tell us this means "end"? return null; } else if (token != JsonToken.START_OBJECT) { throw new IllegalArgumentException("expected JSON Object"); } final Document doc = new Document(); while (true) { token = p.nextToken(); if (token == JsonToken.END_OBJECT) { break; } assert token == JsonToken.FIELD_NAME : token; String fieldName = p.getText(); if (fieldName.equals("fields")) { parseFields(state, doc, p); } else { // Let a plugin handle it: boolean handled = false; for (PostHandle postHandle : postHandlers) { if (postHandle.invoke(state, fieldName, p, doc)) { handled = true; break; } } if (!handled) { throw new IllegalArgumentException("unrecognized field " + p.getText()); } } // nocommit need test that same field name can't // appear more than once? app must put all values for // a given field into an array (for a multi-valued // field) } return doc; }
From source file:com.bazaarvoice.jackson.rison.RisonParser.java
/** * @return Next token from the stream, if any found, or null * to indicate end-of-input/*from w w w .java 2s.c o m*/ */ @Override public JsonToken nextToken() throws IOException, JsonParseException { _numTypesValid = NR_UNKNOWN; if (_currToken == null && _parsingContext.inRoot()) { if (isRisonEnabled(Feature.A_RISON)) { _parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol); return (_currToken = JsonToken.START_ARRAY); } else if (isRisonEnabled(Feature.O_RISON)) { _parsingContext = _parsingContext.createChildObjectContext(_tokenInputRow, _tokenInputCol); return (_currToken = JsonToken.START_OBJECT); } } /* First: field names are special -- we will always tokenize * (part of) value along with field name to simplify * state handling. If so, can and need to use secondary token: */ if (_currToken == JsonToken.FIELD_NAME) { return _nextAfterName(); } if (_tokenIncomplete) { _skipString(); // only strings can be partial } int i = _nextChOrEnd(); if (i < 0) { // end-of-input /* 19-Feb-2009, tatu: Should actually close/release things * like input source, symbol table and recyclable buffers now. */ close(); if (_parsingContext.getParent().inRoot()) { if (isRisonEnabled(Feature.A_RISON) && _parsingContext.inArray()) { _parsingContext = _parsingContext.getParent(); return (_currToken = JsonToken.END_ARRAY); } if (isRisonEnabled(Feature.O_RISON) && _parsingContext.inObject()) { _parsingContext = _parsingContext.getParent(); return (_currToken = JsonToken.END_OBJECT); } } _handleEOF(); return (_currToken = null); } /* First, need to ensure we know the starting location of token * after skipping leading white space */ _tokenInputTotal = _currInputProcessed + _inputPtr - 1; _tokenInputRow = _currInputRow; _tokenInputCol = _inputPtr - _currInputRowStart - 1; // finally: clear any data retained so far _binaryValue = null; // Closing scope? if (i == ')') { if (_parsingContext.inArray()) { _parsingContext = _parsingContext.getParent(); return (_currToken = JsonToken.END_ARRAY); } if (_parsingContext.inObject()) { _parsingContext = _parsingContext.getParent(); return (_currToken = JsonToken.END_OBJECT); } _reportMismatchedEndMarker(i, ')'); } // Nope: do we then expect a comma? if (_parsingContext.expectComma()) { if (i != INT_COMMA) { _reportUnexpectedChar(i, "was expecting comma to separate " + _parsingContext.getTypeDesc() + " entries"); } i = _nextCh(); } /* And should we now have a name? Always true for * Object contexts, since the intermediate 'expect-value' * state is never retained. */ boolean inObject = _parsingContext.inObject(); if (inObject) { // First, field name itself: String name; if (i != INT_APOSTROPHE) { name = _parseUnquotedFieldName(i); } else { name = _parseFieldName(i); } _parsingContext.setCurrentName(name); _currToken = JsonToken.FIELD_NAME; i = _nextCh(); if (i != INT_COLON) { _reportUnexpectedChar(i, "was expecting a colon to separate field name and value"); } i = _nextCh(); } // Ok: we must have a value... what is it? JsonToken t; switch (i) { case INT_APOSTROPHE: _tokenIncomplete = true; t = JsonToken.VALUE_STRING; break; case '(': if (!inObject) { _parsingContext = _parsingContext.createChildObjectContext(_tokenInputRow, _tokenInputCol); } t = JsonToken.START_OBJECT; break; case ')': // Error: ')' is not valid at this point; valid closers have // been handled earlier _reportUnexpectedChar(i, "expected a value"); case INT_MINUS: /* Should we have separate handling for plus? Although * it is not allowed per se, it may be erroneously used, * and could be indicate by a more specific error message. */ case INT_0: case INT_1: case INT_2: case INT_3: case INT_4: case INT_5: case INT_6: case INT_7: case INT_8: case INT_9: t = parseNumberText(i); break; case '!': i = _nextCh(); switch (i) { case '(': if (!inObject) { _parsingContext = _parsingContext.createChildArrayContext(_tokenInputRow, _tokenInputCol); } t = JsonToken.START_ARRAY; break; case INT_t: t = JsonToken.VALUE_TRUE; break; case INT_f: t = JsonToken.VALUE_FALSE; break; case INT_n: t = JsonToken.VALUE_NULL; break; default: t = _handleUnexpectedValue(i); break; } break; default: if (IdentifierUtils.isIdStartLenient(i)) { t = JsonToken.VALUE_STRING; _inputPtr--; // push back the first char _parseUnquotedString(); } else { t = _handleUnexpectedValue(i); } break; } if (inObject) { _nextToken = t; return _currToken; } _currToken = t; return t; }
From source file:com.netflix.hollow.jsonadapter.HollowJsonAdapter.java
private int addSubArray(JsonParser parser, String arrayType, HollowWriteRecord arrayRec) throws IOException { JsonToken token = parser.nextToken(); arrayRec.reset();// w ww . j av a 2s .c o m HollowCollectionSchema schema = (HollowCollectionSchema) hollowSchemas.get(arrayType); ObjectFieldMapping valueRec = null; ObjectMappedFieldPath fieldMapping = null; while (token != JsonToken.END_ARRAY) { int elementOrdinal; if (token == JsonToken.START_OBJECT || token == JsonToken.START_ARRAY) { elementOrdinal = parseSubType(parser, token, schema.getElementType()); } else { if (valueRec == null) { valueRec = getObjectFieldMapping(schema.getElementType()); fieldMapping = valueRec.getSingleFieldMapping(); } addObjectField(parser, token, fieldMapping); elementOrdinal = valueRec.build(-1); } if (arrayRec instanceof HollowListWriteRecord) { ((HollowListWriteRecord) arrayRec).addElement(elementOrdinal); } else { ((HollowSetWriteRecord) arrayRec).addElement(elementOrdinal); } token = parser.nextToken(); } return stateEngine.add(arrayType, arrayRec); }
From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
public void importQnaContent(final JsonParser jsonParser, final Resource resource, final ResourceResolver resolver) throws ServletException, IOException { if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) { jsonParser.nextToken(); // advance to first key in the object - should be the id value of the old post while (jsonParser.getCurrentToken().equals(JsonToken.FIELD_NAME)) { extractTopic(jsonParser, resource, resolver, qnaForumOperations); jsonParser.nextToken(); // get the next token - presumably a field name for the next post }//from w ww . ja v a 2 s .com jsonParser.nextToken(); // skip end token } else { throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got " + jsonParser.getCurrentToken().toString()); } }
From source file:de.undercouch.bson4jackson.BsonParser.java
/** * Can be called when a new embedded document is found. Reads the * document's header and creates a new context on the stack. * @param array true if the document is an embedded array * @return the json token read// w w w . ja v a2 s .co m * @throws IOException if an I/O error occurs */ protected JsonToken handleNewDocument(boolean array) throws IOException { if (_in == null) { //this means Feature.HONOR_DOCUMENT_LENGTH is enabled, and we //haven't yet started reading. Read the first int to find out the //length of the document. byte[] buf = new byte[Integer.SIZE / Byte.SIZE]; int len = 0; while (len < buf.length) { int l = _rawInputStream.read(buf, len, buf.length - len); if (l == -1) { throw new IOException("Not enough bytes for length of document"); } len += l; } //wrap the input stream by a bounded stream, subtract buf.length from the //length because the size itself is included in the length int documentLength = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN).getInt(); InputStream in = new BoundedInputStream(_rawInputStream, documentLength - buf.length); //buffer if the raw input stream is not already buffered if (!(_rawInputStream instanceof BufferedInputStream)) { in = new StaticBufferedInputStream(in); } _counter = new CountingInputStream(in); _in = new LittleEndianInputStream(_counter); } else { //read document header (skip size, we're not interested) _in.readInt(); } _currentContext = new Context(_currentContext, array); return (array ? JsonToken.START_ARRAY : JsonToken.START_OBJECT); }
From source file:com.cinnober.msgcodec.json.JsonCodec.java
/** * Read a static group from the specified stream, when the JSON does not contain the '$type' field. * * @param groupName the expected group name, not null. * @param in the stream to read from, not null. * @return the decoded value.//from w w w .j a va 2 s. c o m * @throws IOException if the underlying stream throws an exception. * @throws DecodeException if the value could not be decoded, or if a required field is missing. */ public Object decodeStatic(String groupName, InputStream in) throws IOException { StaticGroupHandler groupHandler = lookupGroupByName(groupName); if (groupHandler == null) { throw new IllegalArgumentException("Unknown group name"); } JsonFactory f = new JsonFactory(); JsonParser p = f.createParser(in); JsonToken token = p.nextToken(); if (token == JsonToken.VALUE_NULL) { return null; } else if (token != JsonToken.START_OBJECT) { throw new DecodeException("Expected {"); } return groupHandler.readValue(p); }
From source file:de.undercouch.bson4jackson.BsonParserTest.java
/** * Checks if the parser returns a textual representation of arbitrary * tokens. See issue #23./* ww w . ja v a 2 s .c om*/ * @throws Exception if something went wrong */ @Test public void parseAsText() throws Exception { BSONObject o = new BasicBSONObject(); o.put("Float", 5.0f); o.put("Int32", 1234); BSONEncoder enc = new BasicBSONEncoder(); byte[] b = enc.encode(o); ByteArrayInputStream bais = new ByteArrayInputStream(b); BsonFactory fac = new BsonFactory(); BsonParser dec = fac.createParser(bais); assertEquals(JsonToken.START_OBJECT, dec.nextToken()); assertEquals(JsonToken.FIELD_NAME, dec.nextToken()); assertEquals("Float", dec.getCurrentName()); assertEquals(JsonToken.VALUE_NUMBER_FLOAT, dec.nextToken()); assertEquals(5.0f, dec.getFloatValue(), 0.00001); assertEquals("5.0", dec.getText()); assertEquals(JsonToken.FIELD_NAME, dec.nextToken()); assertEquals("Int32", dec.getCurrentName()); assertEquals(JsonToken.VALUE_NUMBER_INT, dec.nextToken()); assertEquals(1234, dec.getIntValue()); assertEquals("1234", dec.getText()); assertEquals(JsonToken.END_OBJECT, dec.nextToken()); }
From source file:com.netflix.hollow.jsonadapter.HollowJsonAdapter.java
private int addStructuredMap(JsonParser parser, String mapTypeName, HollowMapWriteRecord mapRec) throws IOException { JsonToken token = parser.nextToken(); mapRec.reset();/* www. j av a2s .c o m*/ HollowMapSchema schema = (HollowMapSchema) hollowSchemas.get(mapTypeName); while (token != JsonToken.END_ARRAY) { if (token == JsonToken.START_OBJECT) { int keyOrdinal = -1, valueOrdinal = -1; while (token != JsonToken.END_OBJECT) { if (token == JsonToken.START_OBJECT || token == JsonToken.START_ARRAY) { if ("key".equals(parser.getCurrentName())) keyOrdinal = parseSubType(parser, token, schema.getKeyType()); else if ("value".equals(parser.getCurrentName())) valueOrdinal = parseSubType(parser, token, schema.getValueType()); } token = parser.nextToken(); } mapRec.addEntry(keyOrdinal, valueOrdinal); } token = parser.nextToken(); } return stateEngine.add(schema.getName(), mapRec); }
From source file:com.adobe.communities.ugc.migration.importer.UGCImportHelper.java
public void importForumContent(final JsonParser jsonParser, final Resource resource, final ResourceResolver resolver) throws ServletException, IOException { if (jsonParser.getCurrentToken().equals(JsonToken.START_OBJECT)) { jsonParser.nextToken(); // advance to first key in the object - should be the id value of the old post while (jsonParser.getCurrentToken().equals(JsonToken.FIELD_NAME)) { extractTopic(jsonParser, resource, resolver, forumOperations); jsonParser.nextToken(); // get the next token - presumably a field name for the next post }/* w w w .j av a 2 s . c om*/ jsonParser.nextToken(); // skip end token } else { throw new IOException("Improperly formed JSON - expected an OBJECT_START token, but got " + jsonParser.getCurrentToken().toString()); } }