@ -61,6 +61,8 @@ public class JsonConverter {
private static boolean isTypeCastEnabled = true ;
private static boolean isTypeCastEnabled = true ;
private static int maxStringValueLength = 0 ;
public static PostTelemetryMsg convertToTelemetryProto ( JsonElement jsonObject ) throws JsonSyntaxException {
public static PostTelemetryMsg convertToTelemetryProto ( JsonElement jsonObject ) throws JsonSyntaxException {
long systemTs = System . currentTimeMillis ( ) ;
long systemTs = System . currentTimeMillis ( ) ;
PostTelemetryMsg . Builder builder = PostTelemetryMsg . newBuilder ( ) ;
PostTelemetryMsg . Builder builder = PostTelemetryMsg . newBuilder ( ) ;
@ -131,6 +133,10 @@ public class JsonConverter {
if ( element . isJsonPrimitive ( ) ) {
if ( element . isJsonPrimitive ( ) ) {
JsonPrimitive value = element . getAsJsonPrimitive ( ) ;
JsonPrimitive value = element . getAsJsonPrimitive ( ) ;
if ( value . isString ( ) ) {
if ( value . isString ( ) ) {
if ( maxStringValueLength > 0 & & value . getAsString ( ) . length ( ) > maxStringValueLength ) {
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 & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
try {
try {
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
result . add ( buildNumericKeyValueProto ( value , valueEntry . getKey ( ) ) ) ;
@ -389,6 +395,10 @@ public class JsonConverter {
if ( element . isJsonPrimitive ( ) ) {
if ( element . isJsonPrimitive ( ) ) {
JsonPrimitive value = element . getAsJsonPrimitive ( ) ;
JsonPrimitive value = element . getAsJsonPrimitive ( ) ;
if ( value . isString ( ) ) {
if ( value . isString ( ) ) {
if ( maxStringValueLength > 0 & & value . getAsString ( ) . length ( ) > maxStringValueLength ) {
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 & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
if ( isTypeCastEnabled & & NumberUtils . isParsable ( value . getAsString ( ) ) ) {
try {
try {
parseNumericValue ( result , valueEntry , value ) ;
parseNumericValue ( result , valueEntry , value ) ;
@ -456,4 +466,9 @@ public class JsonConverter {
public static void setTypeCastEnabled ( boolean enabled ) {
public static void setTypeCastEnabled ( boolean enabled ) {
isTypeCastEnabled = enabled ;
isTypeCastEnabled = enabled ;
}
}
public static void setMaxStringValueLength ( int length ) {
maxStringValueLength = length ;
}
}
}