@ -19,7 +19,6 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray ;
import com.google.gson.JsonElement ;
import com.google.gson.JsonObject ;
import com.google.gson.JsonParseException ;
import com.google.gson.JsonParser ;
import com.google.gson.JsonPrimitive ;
import com.google.gson.JsonSyntaxException ;
@ -194,62 +193,34 @@ public class JsonConverter {
String message = String . format ( "String value length [%d] for key [%s] is greater than maximum allowed [%d]" , value . getAsString ( ) . length ( ) , valueEntry . getKey ( ) , maxStringValueLength ) ;
throw new JsonSyntaxException ( message ) ;
}
if ( isTypeCastEnabled ) {
if ( NumberUtils . isParsable ( value . getAsString ( ) ) ) {
try {
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
} catch ( RuntimeException th ) {
result . add ( buildStringKVProto ( valueEntry , value ) ) ;
}
} else {
try {
JsonElement jsonElement = JSON_PARSER . parse ( value . getAsString ( ) ) ;
if ( jsonElement . isJsonObject ( ) | | jsonElement . isJsonArray ( ) ) {
result . add ( buildJsonKVProto ( valueEntry , jsonElement ) ) ;
} else {
result . add ( buildStringKVProto ( valueEntry , value ) ) ;
}
} catch ( JsonParseException e ) {
result . add ( buildStringKVProto ( valueEntry , value ) ) ;
}
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
try {
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
} catch ( RuntimeException th ) {
result . add ( KeyValueProto . newBuilder ( ) . setKey ( valueEntry . getKey ( ) ) . setType ( KeyValueType . STRING_V )
. setStringV ( value . getAsString ( ) ) . build ( ) ) ;
}
} else {
result . add ( buildStringKVProto ( valueEntry , value ) ) ;
result . add ( KeyValueProto . newBuilder ( ) . setKey ( valueEntry . getKey ( ) ) . setType ( KeyValueType . STRING_V )
. setStringV ( value . getAsString ( ) ) . build ( ) ) ;
}
} else if ( value . isBoolean ( ) ) {
result . add ( KeyValueProto . newBuilder ( ) . setKey ( valueEntry . getKey ( ) ) . setType ( KeyValueType . BOOLEAN_V )
. setBoolV ( value . getAsBoolean ( ) ) . build ( ) ) ;
} else if ( value . isNumber ( ) ) {
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
} else {
} else if ( ! value . isJsonNull ( ) ) {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + value ) ;
}
} else if ( element . isJsonObject ( ) | | element . isJsonArray ( ) ) {
result . add ( buildJsonKVProto ( valueEntry , element ) ) ;
} else {
result . add ( KeyValueProto . newBuilder ( ) . setKey ( valueEntry . getKey ( ) ) . setType ( KeyValueType . JSON_V ) . setJsonV ( element . toString ( ) ) . build ( ) ) ;
} else if ( ! element . isJsonNull ( ) ) {
throw new JsonSyntaxException ( CAN_T_PARSE_VALUE + element ) ;
}
}
return result ;
}
private static KeyValueProto buildStringKVProto ( Entry < String , JsonElement > valueEntry , JsonPrimitive value ) {
return KeyValueProto . newBuilder ( )
. setKey ( valueEntry . getKey ( ) )
. setType ( KeyValueType . STRING_V )
. setStringV ( value . getAsString ( ) )
. build ( ) ;
}
private static KeyValueProto buildJsonKVProto ( Entry < String , JsonElement > valueEntry , JsonElement jsonElement ) {
return KeyValueProto
. newBuilder ( )
. setKey ( valueEntry . getKey ( ) )
. setType ( KeyValueType . JSON_V )
. setJsonV ( jsonElement . toString ( ) )
. build ( ) ;
}
private static KeyValueProto buildNumericKeyValueProto ( JsonPrimitive value , String key ) {
if ( value . getAsString ( ) . contains ( "." ) ) {
return KeyValueProto . newBuilder ( )