@@ -110,6 +110,28 @@ static void decodeAndWrite(XContentBuilder b, BytesRef r) throws IOException {
110110 }
111111 }
112112
113+ /**
114+ * Decode the value in the passed {@link BytesRef} in place and return it.
115+ * Returns {@link Optional#empty()} for complex values (objects and arrays).
116+ */
117+ static Optional <Object > decode (BytesRef r ) {
118+ return switch ((char ) r .bytes [r .offset ]) {
119+ case BINARY_ENCODING -> Optional .of (TypeUtils .EMBEDDED_OBJECT .decode (r ));
120+ case CBOR_OBJECT_ENCODING , JSON_OBJECT_ENCODING , YAML_OBJECT_ENCODING , SMILE_OBJECT_ENCODING -> Optional .empty ();
121+ case BIG_DECIMAL_ENCODING -> Optional .of (TypeUtils .BIG_DECIMAL .decode (r ));
122+ case FALSE_ENCODING , TRUE_ENCODING -> Optional .of (TypeUtils .BOOLEAN .decode (r ));
123+ case BIG_INTEGER_ENCODING -> Optional .of (TypeUtils .BIG_INTEGER .decode (r ));
124+ case STRING_ENCODING -> Optional .of (TypeUtils .STRING .decode (r ));
125+ case INTEGER_ENCODING -> Optional .of (TypeUtils .INTEGER .decode (r ));
126+ case LONG_ENCODING -> Optional .of (TypeUtils .LONG .decode (r ));
127+ case DOUBLE_ENCODING -> Optional .of (TypeUtils .DOUBLE .decode (r ));
128+ case FLOAT_ENCODING -> Optional .of (TypeUtils .FLOAT .decode (r ));
129+ case NULL_ENCODING -> Optional .ofNullable (TypeUtils .NULL .decode (r ));
130+ case VOID_ENCODING -> Optional .of (TypeUtils .VOID .decode (r ));
131+ default -> throw new IllegalArgumentException ("Can't decode " + r );
132+ };
133+ }
134+
113135 /**
114136 * Determines if the given {@link BytesRef}, encoded with {@link XContentDataHelper#encodeToken(XContentParser)},
115137 * is an encoded object.
@@ -339,6 +361,11 @@ byte[] encode(XContentParser parser) throws IOException {
339361 return bytes ;
340362 }
341363
364+ @ Override
365+ Object decode (BytesRef r ) {
366+ return new BytesRef (r .bytes , r .offset + 1 , r .length - 1 );
367+ }
368+
342369 @ Override
343370 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
344371 b .value (new BytesRef (r .bytes , r .offset + 1 , r .length - 1 ).utf8ToString ());
@@ -359,6 +386,11 @@ byte[] encode(XContentParser parser) throws IOException {
359386 return bytes ;
360387 }
361388
389+ @ Override
390+ Object decode (BytesRef r ) {
391+ return ByteUtils .readIntLE (r .bytes , 1 + r .offset );
392+ }
393+
362394 @ Override
363395 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
364396 b .value (ByteUtils .readIntLE (r .bytes , 1 + r .offset ));
@@ -379,6 +411,11 @@ byte[] encode(XContentParser parser) throws IOException {
379411 return bytes ;
380412 }
381413
414+ @ Override
415+ Object decode (BytesRef r ) {
416+ return ByteUtils .readLongLE (r .bytes , 1 + r .offset );
417+ }
418+
382419 @ Override
383420 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
384421 b .value (ByteUtils .readLongLE (r .bytes , 1 + r .offset ));
@@ -399,6 +436,11 @@ byte[] encode(XContentParser parser) throws IOException {
399436 return bytes ;
400437 }
401438
439+ @ Override
440+ Object decode (BytesRef r ) {
441+ return ByteUtils .readDoubleLE (r .bytes , 1 + r .offset );
442+ }
443+
402444 @ Override
403445 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
404446 b .value (ByteUtils .readDoubleLE (r .bytes , 1 + r .offset ));
@@ -419,6 +461,11 @@ byte[] encode(XContentParser parser) throws IOException {
419461 return bytes ;
420462 }
421463
464+ @ Override
465+ Object decode (BytesRef r ) {
466+ return ByteUtils .readFloatLE (r .bytes , 1 + r .offset );
467+ }
468+
422469 @ Override
423470 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
424471 b .value (ByteUtils .readFloatLE (r .bytes , 1 + r .offset ));
@@ -437,6 +484,11 @@ byte[] encode(XContentParser parser) throws IOException {
437484 return bytes ;
438485 }
439486
487+ @ Override
488+ Object decode (BytesRef r ) {
489+ return new BigInteger (r .bytes , r .offset + 1 , r .length - 1 );
490+ }
491+
440492 @ Override
441493 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
442494 b .value (new BigInteger (r .bytes , r .offset + 1 , r .length - 1 ));
@@ -455,6 +507,15 @@ byte[] encode(XContentParser parser) throws IOException {
455507 return bytes ;
456508 }
457509
510+ @ Override
511+ Object decode (BytesRef r ) {
512+ if (r .length < 5 ) {
513+ throw new IllegalArgumentException ("Can't decode " + r );
514+ }
515+ int scale = ByteUtils .readIntLE (r .bytes , r .offset + 1 );
516+ return new BigDecimal (new BigInteger (r .bytes , r .offset + 5 , r .length - 5 ), scale );
517+ }
518+
458519 @ Override
459520 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
460521 if (r .length < 5 ) {
@@ -477,6 +538,15 @@ byte[] encode(XContentParser parser) throws IOException {
477538 return bytes ;
478539 }
479540
541+ @ Override
542+ Object decode (BytesRef r ) {
543+ if (r .length != 1 ) {
544+ throw new IllegalArgumentException ("Can't decode " + r );
545+ }
546+ assert r .bytes [r .offset ] == 't' || r .bytes [r .offset ] == 'f' : r .bytes [r .offset ];
547+ return r .bytes [r .offset ] == 't' ;
548+ }
549+
480550 @ Override
481551 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
482552 if (r .length != 1 ) {
@@ -499,6 +569,11 @@ byte[] encode(XContentParser parser) throws IOException {
499569 return bytes ;
500570 }
501571
572+ @ Override
573+ Object decode (BytesRef r ) {
574+ return null ;
575+ }
576+
502577 @ Override
503578 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
504579 b .nullValue ();
@@ -517,6 +592,11 @@ byte[] encode(XContentParser parser) throws IOException {
517592 return bytes ;
518593 }
519594
595+ @ Override
596+ Object decode (BytesRef r ) {
597+ return new BytesRef (r .bytes , r .offset + 1 , r .length - 1 );
598+ }
599+
520600 @ Override
521601 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
522602 b .value (r .bytes , r .offset + 1 , r .length - 1 );
@@ -538,6 +618,11 @@ byte[] encode(XContentParser parser) throws IOException {
538618 }
539619 }
540620
621+ @ Override
622+ Object decode (BytesRef r ) {
623+ throw new UnsupportedOperationException ();
624+ }
625+
541626 @ Override
542627 void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException {
543628 switch ((char ) r .bytes [r .offset ]) {
@@ -562,6 +647,11 @@ byte[] encode(XContentParser parser) {
562647 return bytes ;
563648 }
564649
650+ @ Override
651+ Object decode (BytesRef r ) {
652+ throw new UnsupportedOperationException ();
653+ }
654+
565655 @ Override
566656 void decodeAndWrite (XContentBuilder b , BytesRef r ) {
567657 // NOOP
@@ -591,6 +681,8 @@ void assertValidEncoding(byte[] encodedValue) {
591681
592682 abstract byte [] encode (XContentParser parser ) throws IOException ;
593683
684+ abstract Object decode (BytesRef r );
685+
594686 abstract void decodeAndWrite (XContentBuilder b , BytesRef r ) throws IOException ;
595687
596688 static byte [] encode (BigInteger n , Byte encoding ) throws IOException {
0 commit comments