@ -16,6 +16,8 @@
@@ -16,6 +16,8 @@
*/
package kafka.utils
import java.nio.charset.StandardCharsets
import org.junit.Assert._
import org.junit.Test
import com.fasterxml.jackson.databind.JsonNode
@ -47,7 +49,7 @@ class JsonTest {
@@ -47,7 +49,7 @@ class JsonTest {
// Test with encoder that properly escapes backslash and quotes
val map = Map ( "foo1" -> """bar1\,bar2""" , "foo2" -> """\bar""" )
val encoded = Json . encode ( map )
val encoded = Json . l egacyE ncodeAsString ( map )
val decoded = Json . parseFull ( encoded )
assertEquals ( Json . parseFull ( """{"foo1":"bar1\\,bar2", "foo2":"\\bar"}""" ) , decoded )
@ -57,24 +59,71 @@ class JsonTest {
@@ -57,24 +59,71 @@ class JsonTest {
}
@Test
def testJsonEncoding ( ) {
assertEquals ( "null" , Json . encode ( null ) )
assertEquals ( "1" , Json . encode ( 1 ) )
assertEquals ( "1" , Json . encode ( 1L ) )
assertEquals ( "1" , Json . encode ( 1. toByte ) )
assertEquals ( "1" , Json . encode ( 1. toShort ) )
assertEquals ( "1.0" , Json . encode ( 1.0 ) )
assertEquals ( "\"str\"" , Json . encode ( "str" ) )
assertEquals ( "true" , Json . encode ( true ) )
assertEquals ( "false" , Json . encode ( false ) )
assertEquals ( "[]" , Json . encode ( Seq ( ) ) )
assertEquals ( "[1,2,3]" , Json . encode ( Seq ( 1 , 2 , 3 ) ) )
assertEquals ( "[1,\"2\",[3]]" , Json . encode ( Seq ( 1 , "2" , Seq ( 3 ) ) ) )
assertEquals ( "{}" , Json . encode ( Map ( ) ) )
assertEquals ( "{\"a\":1,\"b\":2}" , Json . encode ( Map ( "a" -> 1 , "b" -> 2 ) ) )
assertEquals ( "{\"a\":[1,2],\"c\":[3,4]}" , Json . encode ( Map ( "a" -> Seq ( 1 , 2 ) , "c" -> Seq ( 3 , 4 ) ) ) )
assertEquals ( """"str1\\,str2"""" , Json . encode ( """str1\,str2""" ) )
assertEquals ( """"\"quoted\""""" , Json . encode ( """"quoted"""" ) )
def testLegacyEncodeAsString ( ) {
assertEquals ( "null" , Json . legacyEncodeAsString ( null ) )
assertEquals ( "1" , Json . legacyEncodeAsString ( 1 ) )
assertEquals ( "1" , Json . legacyEncodeAsString ( 1L ) )
assertEquals ( "1" , Json . legacyEncodeAsString ( 1. toByte ) )
assertEquals ( "1" , Json . legacyEncodeAsString ( 1. toShort ) )
assertEquals ( "1.0" , Json . legacyEncodeAsString ( 1.0 ) )
assertEquals ( """"str"""" , Json . legacyEncodeAsString ( "str" ) )
assertEquals ( "true" , Json . legacyEncodeAsString ( true ) )
assertEquals ( "false" , Json . legacyEncodeAsString ( false ) )
assertEquals ( "[]" , Json . legacyEncodeAsString ( Seq ( ) ) )
assertEquals ( "[1,2,3]" , Json . legacyEncodeAsString ( Seq ( 1 , 2 , 3 ) ) )
assertEquals ( """[1,"2",[3]]""" , Json . legacyEncodeAsString ( Seq ( 1 , "2" , Seq ( 3 ) ) ) )
assertEquals ( "{}" , Json . legacyEncodeAsString ( Map ( ) ) )
assertEquals ( """{"a":1,"b":2}""" , Json . legacyEncodeAsString ( Map ( "a" -> 1 , "b" -> 2 ) ) )
assertEquals ( """{"a":[1,2],"c":[3,4]}""" , Json . legacyEncodeAsString ( Map ( "a" -> Seq ( 1 , 2 ) , "c" -> Seq ( 3 , 4 ) ) ) )
assertEquals ( """"str1\\,str2"""" , Json . legacyEncodeAsString ( """str1\,str2""" ) )
assertEquals ( """"\"quoted\""""" , Json . legacyEncodeAsString ( """"quoted"""" ) )
}
@Test
def testEncodeAsString ( ) {
assertEquals ( "null" , Json . encodeAsString ( null ) )
assertEquals ( "1" , Json . encodeAsString ( 1 ) )
assertEquals ( "1" , Json . encodeAsString ( 1L ) )
assertEquals ( "1" , Json . encodeAsString ( 1. toByte ) )
assertEquals ( "1" , Json . encodeAsString ( 1. toShort ) )
assertEquals ( "1.0" , Json . encodeAsString ( 1.0 ) )
assertEquals ( """"str"""" , Json . encodeAsString ( "str" ) )
assertEquals ( "true" , Json . encodeAsString ( true ) )
assertEquals ( "false" , Json . encodeAsString ( false ) )
assertEquals ( "[]" , Json . encodeAsString ( Seq ( ) . asJava ) )
assertEquals ( "[null]" , Json . encodeAsString ( Seq ( null ) . asJava ) )
assertEquals ( "[1,2,3]" , Json . encodeAsString ( Seq ( 1 , 2 , 3 ) . asJava ) )
assertEquals ( """[1,"2",[3],null]""" , Json . encodeAsString ( Seq ( 1 , "2" , Seq ( 3 ) . asJava , null ) . asJava ) )
assertEquals ( "{}" , Json . encodeAsString ( Map ( ) . asJava ) )
assertEquals ( """{"a":1,"b":2,"c":null}""" , Json . encodeAsString ( Map ( "a" -> 1 , "b" -> 2 , "c" -> null ) . asJava ) )
assertEquals ( """{"a":[1,2],"c":[3,4]}""" , Json . encodeAsString ( Map ( "a" -> Seq ( 1 , 2 ) . asJava , "c" -> Seq ( 3 , 4 ) . asJava ) . asJava ) )
assertEquals ( """{"a":[1,2],"b":[3,4],"c":null}""" , Json . encodeAsString ( Map ( "a" -> Seq ( 1 , 2 ) . asJava , "b" -> Seq ( 3 , 4 ) . asJava , "c" -> null ) . asJava ) )
assertEquals ( """"str1\\,str2"""" , Json . encodeAsString ( """str1\,str2""" ) )
assertEquals ( """"\"quoted\""""" , Json . encodeAsString ( """"quoted"""" ) )
}
@Test
def testEncodeAsBytes ( ) {
assertEquals ( "null" , new String ( Json . encodeAsBytes ( null ) , StandardCharsets . UTF_8 ) )
assertEquals ( "1" , new String ( Json . encodeAsBytes ( 1 ) , StandardCharsets . UTF_8 ) )
assertEquals ( "1" , new String ( Json . encodeAsBytes ( 1L ) , StandardCharsets . UTF_8 ) )
assertEquals ( "1" , new String ( Json . encodeAsBytes ( 1. toByte ) , StandardCharsets . UTF_8 ) )
assertEquals ( "1" , new String ( Json . encodeAsBytes ( 1. toShort ) , StandardCharsets . UTF_8 ) )
assertEquals ( "1.0" , new String ( Json . encodeAsBytes ( 1.0 ) , StandardCharsets . UTF_8 ) )
assertEquals ( """"str"""" , new String ( Json . encodeAsBytes ( "str" ) , StandardCharsets . UTF_8 ) )
assertEquals ( "true" , new String ( Json . encodeAsBytes ( true ) , StandardCharsets . UTF_8 ) )
assertEquals ( "false" , new String ( Json . encodeAsBytes ( false ) , StandardCharsets . UTF_8 ) )
assertEquals ( "[]" , new String ( Json . encodeAsBytes ( Seq ( ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( "[null]" , new String ( Json . encodeAsBytes ( Seq ( null ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( "[1,2,3]" , new String ( Json . encodeAsBytes ( Seq ( 1 , 2 , 3 ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( """[1,"2",[3],null]""" , new String ( Json . encodeAsBytes ( Seq ( 1 , "2" , Seq ( 3 ) . asJava , null ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( "{}" , new String ( Json . encodeAsBytes ( Map ( ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( """{"a":1,"b":2,"c":null}""" , new String ( Json . encodeAsBytes ( Map ( "a" -> 1 , "b" -> 2 , "c" -> null ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( """{"a":[1,2],"c":[3,4]}""" , new String ( Json . encodeAsBytes ( Map ( "a" -> Seq ( 1 , 2 ) . asJava , "c" -> Seq ( 3 , 4 ) . asJava ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( """{"a":[1,2],"b":[3,4],"c":null}""" , new String ( Json . encodeAsBytes ( Map ( "a" -> Seq ( 1 , 2 ) . asJava , "b" -> Seq ( 3 , 4 ) . asJava , "c" -> null ) . asJava ) , StandardCharsets . UTF_8 ) )
assertEquals ( """"str1\\,str2"""" , new String ( Json . encodeAsBytes ( """str1\,str2""" ) , StandardCharsets . UTF_8 ) )
assertEquals ( """"\"quoted\""""" , new String ( Json . encodeAsBytes ( """"quoted"""" ) , StandardCharsets . UTF_8 ) )
}
}