Hi,
I have an issue regarding the use of the org.json.me#json
Java library and Java String containing chinese characters. The following piece of code build a JSON Java String with chinese characters. Everything seems to be ok when I print the characters array. But, if I try to store this JSON String to a file and then read it again, the result is not equals to the original string (the 2 characters array are not the same). Can you help me to understand ?
Best regards,
Jean Morin
Source code:
package jsontest;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.json.me.JSONException;
import org.json.me.JSONObject;
public class JSONTest {
public static void main(String[] args) throws FileNotFoundException, IOException, JSONException {
// create a simple JSON String using chinese characters
String jsonString = createSimpleJSONString("message", "MyMessage_雷斯考");
System.out.println("JSON characters (Original String):");
printStringCharArray(jsonString);
// write JSON message with chinese characters in a file
File f = createNewFile("test.txt"); //$NON-NLS-1$
writeInFile(f, jsonString.getBytes());
// read JSON message previously written in the file
byte[] readJsonStringBytes = readBytesFromFile(f);
String readJsonString = new String(readJsonStringBytes);
System.out.println();
System.out.println("JSON characters (String read from file):");
printStringCharArray(readJsonString);
System.out.println();
if (readJsonString.equals(jsonString)) {
System.out.println("Strings are equals");
} else {
System.out.println("Strings are not equals");
}
}
private static String createSimpleJSONString(String key, String value) throws JSONException {
JSONObject jsonObj = new JSONObject();
jsonObj.put(key, value);
return jsonObj.toString();
}
private static void writeInFile(File f, byte[] data) throws FileNotFoundException, IOException {
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(data);
}
}
private static byte[] readBytesFromFile(File file) throws FileNotFoundException, IOException {
byte[] buffer = new byte[32];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (FileInputStream fis = new FileInputStream(file)) {
int nbRead = -1;
while ((nbRead = fis.read(buffer)) != -1) {
bos.write(buffer, 0, nbRead);
}
}
return bos.toByteArray();
}
private static File createNewFile(String path) throws IOException {
File f = new File(path);
if (f.exists()) {
f.delete();
}
f.createNewFile();
return f;
}
private static void printStringCharArray(String s) {
for (char c : s.toCharArray()) {
System.out.print("0x" + Integer.toHexString(c & 0xffff) + " ");
}
System.out.println();
}
}
Console output:
JSON characters (Original String):
0x7b 0x22 0x6d 0x65 0x73 0x73 0x61 0x67 0x65 0x22 0x3a 0x22 0x4d 0x79 0x4d 0x65 0x73 0x73 0x61 0x67 0x65 0x5f 0x96f7 0x65af 0x8003 0x22 0x7d
JSON characters (String read from file):
0x7b 0x22 0x6d 0x65 0x73 0x73 0x61 0x67 0x65 0x22 0x3a 0x22 0x4d 0x79 0x4d 0x65 0x73 0x73 0x61 0x67 0x65 0x5f 0xfff7 0xffaf 0x3 0x22 0x7d
Strings are not equals