Merge pull request #9589 from thingsboard/fixBugStringAsObjectToBytes
[fix][bug]String asObjectToBytes
This commit is contained in:
commit
b70e2adeb5
@ -61,9 +61,9 @@ public class TbUtils {
|
|||||||
parserConfig.addImport("decodeToJson", new MethodStub(TbUtils.class.getMethod("decodeToJson",
|
parserConfig.addImport("decodeToJson", new MethodStub(TbUtils.class.getMethod("decodeToJson",
|
||||||
ExecutionContext.class, String.class)));
|
ExecutionContext.class, String.class)));
|
||||||
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes",
|
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes",
|
||||||
ExecutionContext.class, String.class)));
|
ExecutionContext.class, Object.class)));
|
||||||
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes",
|
parserConfig.addImport("stringToBytes", new MethodStub(TbUtils.class.getMethod("stringToBytes",
|
||||||
ExecutionContext.class, String.class, String.class)));
|
ExecutionContext.class, Object.class, String.class)));
|
||||||
parserConfig.registerNonConvertableMethods(TbUtils.class, Collections.singleton("stringToBytes"));
|
parserConfig.registerNonConvertableMethods(TbUtils.class, Collections.singleton("stringToBytes"));
|
||||||
parserConfig.addImport("parseInt", new MethodStub(TbUtils.class.getMethod("parseInt",
|
parserConfig.addImport("parseInt", new MethodStub(TbUtils.class.getMethod("parseInt",
|
||||||
String.class)));
|
String.class)));
|
||||||
@ -192,14 +192,22 @@ public class TbUtils {
|
|||||||
return new String(bytes, charsetName);
|
return new String(bytes, charsetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Byte> stringToBytes(ExecutionContext ctx, String str) {
|
public static List<Byte> stringToBytes(ExecutionContext ctx, Object str) throws IllegalAccessException {
|
||||||
byte[] bytes = str.getBytes();
|
if (str instanceof String) {
|
||||||
|
byte[] bytes = str.toString().getBytes();
|
||||||
return bytesToList(ctx, bytes);
|
return bytesToList(ctx, bytes);
|
||||||
|
} else {
|
||||||
|
throw new IllegalAccessException("Invalid type parameter [" + str.getClass().getSimpleName() + "]. Expected 'String'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<Byte> stringToBytes(ExecutionContext ctx, String str, String charsetName) throws UnsupportedEncodingException {
|
public static List<Byte> stringToBytes(ExecutionContext ctx, Object str, String charsetName) throws UnsupportedEncodingException, IllegalAccessException {
|
||||||
byte[] bytes = str.getBytes(charsetName);
|
if (str instanceof String) {
|
||||||
|
byte[] bytes = str.toString().getBytes(charsetName);
|
||||||
return bytesToList(ctx, bytes);
|
return bytesToList(ctx, bytes);
|
||||||
|
} else {
|
||||||
|
throw new IllegalAccessException("Invalid type parameter [" + str.getClass().getSimpleName() + "]. Expected 'String'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static byte[] bytesFromList(List<Byte> bytesList) {
|
private static byte[] bytesFromList(List<Byte> bytesList) {
|
||||||
|
|||||||
@ -350,7 +350,7 @@ public class TbUtilsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void parseBytesDecodeToJson() throws IOException {
|
public void parseBytesDecodeToJson() throws IOException, IllegalAccessException {
|
||||||
String expectedStr = "{\"hello\": \"world\"}";
|
String expectedStr = "{\"hello\": \"world\"}";
|
||||||
ExecutionHashMap<String, Object> expectedJson = new ExecutionHashMap<>(1, ctx);
|
ExecutionHashMap<String, Object> expectedJson = new ExecutionHashMap<>(1, ctx);
|
||||||
expectedJson.put("hello", "world");
|
expectedJson.put("hello", "world");
|
||||||
@ -367,6 +367,24 @@ public class TbUtilsTest {
|
|||||||
Assert.assertEquals(expectedJson,actualJson);
|
Assert.assertEquals(expectedJson,actualJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void stringToBytesInputObjectTest() throws IOException, IllegalAccessException {
|
||||||
|
String expectedStr = "{\"hello\": \"world\"}";
|
||||||
|
Object inputJson = TbUtils.decodeToJson(ctx, expectedStr);
|
||||||
|
byte[] arrayBytes = {119, 111, 114, 108, 100};
|
||||||
|
List<Byte> expectedBytes = Bytes.asList(arrayBytes);
|
||||||
|
List<Byte> actualBytes = TbUtils.stringToBytes(ctx, ((ExecutionHashMap) inputJson).get("hello"));
|
||||||
|
Assert.assertEquals(expectedBytes, actualBytes);
|
||||||
|
actualBytes = TbUtils.stringToBytes(ctx, ((ExecutionHashMap) inputJson).get("hello"), "UTF-8");
|
||||||
|
Assert.assertEquals(expectedBytes, actualBytes);
|
||||||
|
|
||||||
|
expectedStr = "{\"hello\": 1234}";
|
||||||
|
inputJson = TbUtils.decodeToJson(ctx, expectedStr);
|
||||||
|
Object finalInputJson = inputJson;
|
||||||
|
Assert.assertThrows(IllegalAccessException.class, () -> TbUtils.stringToBytes(ctx, ((ExecutionHashMap) finalInputJson).get("hello")));
|
||||||
|
Assert.assertThrows(IllegalAccessException.class, () -> TbUtils.stringToBytes(ctx, ((ExecutionHashMap) finalInputJson).get("hello"), "UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
private static List<Byte> toList(byte[] data) {
|
private static List<Byte> toList(byte[] data) {
|
||||||
List<Byte> result = new ArrayList<>(data.length);
|
List<Byte> result = new ArrayList<>(data.length);
|
||||||
for (Byte b : data) {
|
for (Byte b : data) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user