@@ -273,38 +273,37 @@ public static FileInfo fromXContent(XContentParser parser) throws IOException {
273273 Version writtenBy = null ;
274274 String writtenByStr = null ;
275275 BytesRef metaHash = new BytesRef ();
276- if (token == XContentParser .Token .START_OBJECT ) {
277- while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
278- if (token == XContentParser .Token .FIELD_NAME ) {
279- String currentFieldName = parser .currentName ();
280- token = parser .nextToken ();
281- if (token .isValue ()) {
282- if (NAME .equals (currentFieldName )) {
283- name = parser .text ();
284- } else if (PHYSICAL_NAME .equals (currentFieldName )) {
285- physicalName = parser .text ();
286- } else if (LENGTH .equals (currentFieldName )) {
287- length = parser .longValue ();
288- } else if (CHECKSUM .equals (currentFieldName )) {
289- checksum = parser .text ();
290- } else if (PART_SIZE .equals (currentFieldName )) {
291- partSize = new ByteSizeValue (parser .longValue ());
292- } else if (WRITTEN_BY .equals (currentFieldName )) {
293- writtenByStr = parser .text ();
294- writtenBy = Lucene .parseVersionLenient (writtenByStr , null );
295- } else if (META_HASH .equals (currentFieldName )) {
296- metaHash .bytes = parser .binaryValue ();
297- metaHash .offset = 0 ;
298- metaHash .length = metaHash .bytes .length ;
299- } else {
300- throw new ElasticsearchParseException ("unknown parameter [{}]" , currentFieldName );
301- }
276+ XContentParserUtils .ensureExpectedToken (token , XContentParser .Token .START_OBJECT , parser );
277+ while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
278+ if (token == XContentParser .Token .FIELD_NAME ) {
279+ String currentFieldName = parser .currentName ();
280+ token = parser .nextToken ();
281+ if (token .isValue ()) {
282+ if (NAME .equals (currentFieldName )) {
283+ name = parser .text ();
284+ } else if (PHYSICAL_NAME .equals (currentFieldName )) {
285+ physicalName = parser .text ();
286+ } else if (LENGTH .equals (currentFieldName )) {
287+ length = parser .longValue ();
288+ } else if (CHECKSUM .equals (currentFieldName )) {
289+ checksum = parser .text ();
290+ } else if (PART_SIZE .equals (currentFieldName )) {
291+ partSize = new ByteSizeValue (parser .longValue ());
292+ } else if (WRITTEN_BY .equals (currentFieldName )) {
293+ writtenByStr = parser .text ();
294+ writtenBy = Lucene .parseVersionLenient (writtenByStr , null );
295+ } else if (META_HASH .equals (currentFieldName )) {
296+ metaHash .bytes = parser .binaryValue ();
297+ metaHash .offset = 0 ;
298+ metaHash .length = metaHash .bytes .length ;
302299 } else {
303- throw new ElasticsearchParseException ( "unexpected token [{}]" , token );
300+ XContentParserUtils . throwUnknownField ( currentFieldName , parser . getTokenLocation () );
304301 }
305302 } else {
306- throw new ElasticsearchParseException ( "unexpected token [{}]" , token );
303+ XContentParserUtils . throwUnknownToken ( token , parser . getTokenLocation () );
307304 }
305+ } else {
306+ XContentParserUtils .throwUnknownToken (token , parser .getTokenLocation ());
308307 }
309308 }
310309
@@ -513,39 +512,38 @@ public static BlobStoreIndexShardSnapshot fromXContent(XContentParser parser) th
513512 parser .nextToken ();
514513 }
515514 XContentParser .Token token = parser .currentToken ();
516- if (token == XContentParser .Token .START_OBJECT ) {
517- while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
518- XContentParserUtils .ensureExpectedToken (XContentParser .Token .FIELD_NAME , token , parser );
519- final String currentFieldName = parser .currentName ();
520- token = parser .nextToken ();
521- if (token .isValue ()) {
522- if (PARSE_NAME .match (currentFieldName , parser .getDeprecationHandler ())) {
523- snapshot = parser .text ();
524- } else if (PARSE_INDEX_VERSION .match (currentFieldName , parser .getDeprecationHandler ())) {
525- // The index-version is needed for backward compatibility with v 1.0
526- indexVersion = parser .longValue ();
527- } else if (PARSE_START_TIME .match (currentFieldName , parser .getDeprecationHandler ())) {
528- startTime = parser .longValue ();
529- } else if (PARSE_TIME .match (currentFieldName , parser .getDeprecationHandler ())) {
530- time = parser .longValue ();
531- } else if (PARSE_INCREMENTAL_FILE_COUNT .match (currentFieldName , parser .getDeprecationHandler ())) {
532- incrementalFileCount = parser .intValue ();
533- } else if (PARSE_INCREMENTAL_SIZE .match (currentFieldName , parser .getDeprecationHandler ())) {
534- incrementalSize = parser .longValue ();
535- } else {
536- throw new ElasticsearchParseException ("unknown parameter [{}]" , currentFieldName );
537- }
538- } else if (token == XContentParser .Token .START_ARRAY ) {
539- if (PARSE_FILES .match (currentFieldName , parser .getDeprecationHandler ())) {
540- while ((parser .nextToken ()) != XContentParser .Token .END_ARRAY ) {
541- indexFiles .add (FileInfo .fromXContent (parser ));
542- }
543- } else {
544- throw new ElasticsearchParseException ("unknown parameter [{}]" , currentFieldName );
515+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .START_OBJECT , token , parser );
516+ while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ) {
517+ XContentParserUtils .ensureExpectedToken (XContentParser .Token .FIELD_NAME , token , parser );
518+ final String currentFieldName = parser .currentName ();
519+ token = parser .nextToken ();
520+ if (token .isValue ()) {
521+ if (PARSE_NAME .match (currentFieldName , parser .getDeprecationHandler ())) {
522+ snapshot = parser .text ();
523+ } else if (PARSE_INDEX_VERSION .match (currentFieldName , parser .getDeprecationHandler ())) {
524+ // The index-version is needed for backward compatibility with v 1.0
525+ indexVersion = parser .longValue ();
526+ } else if (PARSE_START_TIME .match (currentFieldName , parser .getDeprecationHandler ())) {
527+ startTime = parser .longValue ();
528+ } else if (PARSE_TIME .match (currentFieldName , parser .getDeprecationHandler ())) {
529+ time = parser .longValue ();
530+ } else if (PARSE_INCREMENTAL_FILE_COUNT .match (currentFieldName , parser .getDeprecationHandler ())) {
531+ incrementalFileCount = parser .intValue ();
532+ } else if (PARSE_INCREMENTAL_SIZE .match (currentFieldName , parser .getDeprecationHandler ())) {
533+ incrementalSize = parser .longValue ();
534+ } else {
535+ XContentParserUtils .throwUnknownField (currentFieldName , parser .getTokenLocation ());
536+ }
537+ } else if (token == XContentParser .Token .START_ARRAY ) {
538+ if (PARSE_FILES .match (currentFieldName , parser .getDeprecationHandler ())) {
539+ while ((parser .nextToken ()) != XContentParser .Token .END_ARRAY ) {
540+ indexFiles .add (FileInfo .fromXContent (parser ));
545541 }
546542 } else {
547- throw new ElasticsearchParseException ( "unexpected token [{}]" , token );
543+ XContentParserUtils . throwUnknownField ( currentFieldName , parser . getTokenLocation () );
548544 }
545+ } else {
546+ XContentParserUtils .throwUnknownToken (token , parser .getTokenLocation ());
549547 }
550548 }
551549
0 commit comments