Using Gson to serialize strings with \n in them - java

\n in a string is effective in printing the text following \n in next line. However, if the same string is serialized using Gson, then \n is no more effective in printing it in next line. How can we fix this ? Sample program given below.
In the program below, output of toString on map is printing text in next line due to presence of \n. However, the json string serialized using Gson is not able to show the same behaviour. In serialized string i.e. gsonOutput variable, '\' and 'n' are getting treated as separate characters, due to which the text after \n is not getting printed in next line. How can we fix this in gson serialization ?
Program:
Map<String, String> map = new HashMap<String,String>();
map.put("x", "First_Line\nWant_This_To_Be_Printed_In_Next_Line");
final String gsonOutput = new Gson().toJson(map);
final String toStringOutput = map.toString();
System.out.println("gsonOutput:" + gsonOutput);
System.out.println("toStringOutput:" + toStringOutput);
Output:
gsonOutput:{"x":"First_Line\nWant_This_To_Be_Printed_In_Next_Line"}
toStringOutput:{x=First_Line
Want_This_To_Be_Printed_In_Next_Line}

I am guessing that the gsonOutput has escaped the new line so if you change the line
final String gsonOutput = new Gson().toJson(map);
to (to unescape it):
final String gsonOutput = new Gson().toJson(map).replace("\\n", "\n");
you will get the output
gsonOutput:{"x":"First_Line
Want_This_To_Be_Printed_In_Next_Line_With_A_Tab_Before_It"}
toStringOutput:{x=First_Line
Want_This_To_Be_Printed_In_Next_Line_With_A_Tab_Before_It}
There probably is a better way of doing this :-)

Related

Android convert diamond question marks to UTF-8 Arabic string

I'm using an API that sends and receives raw bytes.
But i have problem with displaying the Arabic words that comes over the API, it's displaying like diamond question marks "���"
I've tried to convert the string from and to utf-8.
This example returns question marks but not inside the black square "??? ???" :
String str = new String(originalStr.getBytes("ISO-8859-1"), "UTF-8");
This one returns empty string :
String str = new String(originalStr.getBytes("WINDOWS-1256"), "UTF-8");
And this one also returns an empty string :
String str = new String(originalStr.getBytes("WINDOWS-1252"), "UTF-8");
I've succeded to display the Arabic words in PHP by converting from cp1256 to utf-8 :
echo iconv('cp1256', 'utf-8', $string);
The correct character encoding for Arabic is cp1256
How can i achieve that?

Csv: search for String and replace with another string

I have a .csv file that contains:
scenario, custom, master_data
1, ${CUSTOM}, A_1
I have a string:
a, b, c
and I want to replace 'custom' with 'a, b, c'. How can I do that and save to the existing .csv file?
Probably the easiest way is to read in one file and output to another file as you go, modifying it on a per-line basis
You could try something with tokenizers, this may not be completely correct for your output/input, but you can adapt it to your CSV file formatting
BufferedReader reader = new BufferedReader(new FileReader("input.csv"));
BufferedWriter writer = new BufferedWriter(new FileWriter("output.csv"));
String custom = "custom";
String replace = "a, b, c";
for(String line = reader.readLine(); line != null; line = reader.readLine())
{
String output = "";
StringTokenizer tokenizer = new StringTokenizer(line, ",");
for(String token = tokenizer.nextToken(); tokenizer.hasMoreTokens(); token = tokenizer.nextToken())
if(token.equals(custom)
output = "," + replace;
else
output = "," + token;
}
readInventory.close();
If this is for a one off thing, it also has the benefit of not having to research regular expressions (which are quite powerful and useful, good to know, but maybe for a later date?)
Have a look at Can you recommend a Java library for reading (and possibly writing) CSV files?
And once the values have been read, search for strings / value that start with ${ and end with }. Use Java Regular Expressions like \$\{(\w)\}. Then use some map for looking up the found key, and the related value. Java Properties would be a good candidate.
Then write a new csv file.
Since your replacement string is quite unique you can do it quickly without complicated parsing by just reading your file into a buffer, and then converting that buffer into a string. Replace all occurrences of the text you wish to replace with your target text. Then convert the string to a buffer and write that back to the file...
Pattern.quote is required because your string is a regular expression. If you don't quote it you may run into unexpected results.
Also it's generally not smart to overwrite your source file. Best is to create a new file then delete the old and rename the new to the old. Any error halfway will then not delete all your data.
final Path yourPath = Paths.get("Your path");
byte[] buff = Files.readAllBytes(yourPath);
String s = new String(buff, Charset.defaultCharset());
s = s.replaceAll(Pattern.quote("${CUSTOM}"), "a, b, c");
Files.write(yourPath, s.getBytes());

convert string ["string"] to string array

I have a kind of strange problem, I am receiving from server side an compressed text that is a string array, for exemple ["str1","str2"] or just ["str"]
Can I convert it to an normal string array? like:
String[] array;
array[1] = "str";
I know that is not a big deal to convert an simple string but not this one...Any ideas?
This text can be treated as JSON so you could try using JSON parser of your choice. For gson your code could look like.
String text = "[\"str1\",\"str2\"]"; // represents ["str1","str2"]
Gson gson = new Gson();
String[] array = gson.fromJson(text, String[].class);
System.out.println(array[0]); //str1
System.out.println(array[1]); //str2
If you are able to change the way server is sending you informations you can consider sending array object, instead of text representing array content. More info at
https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html
http://www.tutorialspoint.com/java/java_serialization.htm
or many other Java tutorials under serialization/deserialization.
This may help you:
// cleanup the line from [ and ]
String regx = "[]";
char[] ca = regx.toCharArray();
for (char c : ca) {
line = line.replace("" + c, "");
}
String[] strings = line.split("\\s*,\\s*");
// now you have your string array

Java Output UTF-8 to Real Characters?

In Java, how can I output UTF-8 to real string?
我们
\u6211\u4eec
String str = new String("\u6211\u4eec");
System.out.println(str); // still ouput \u6211\u4eec, but I expect 我们 to be an output
-----
String tmp = request.getParameter("tag");
System.out.println("request:"+tmp);
System.out.println("character set :"+request.getCharacterEncoding());
String tmp1 = new String("\u6211\u4eec");
System.out.println("string equal:"+(tmp.equalsIgnoreCase(tmp1)));
String tag = new String(tmp);
System.out.println(tag);
request:\u6211\u4eec
character set :UTF-8
string equal:false
\u6211\u4eec
From the output, the value from the request is the same as the string value of tmp1, but why does equalsIgnoreCase output false?
did you try to display just one of them? like
String str = new String("\u6211");
System.out.println(str);
I bet there is a problem in how you create that string.
Java String are encoded in UTF-16. I do not see any problem in your code, I would believe the problem comes from your console and it doesn't show correctly the content of the String.
If you are using eclipse, change your console encoding here to UTF-8
Eclipse > Preferences > General > Workspace > Text file encoding

concatenate a string and a null character

I am completely new to java, and I am trying to join to string together with a null character between them.
String header = "text 19"
String content = "this is the content"
String wholething = // this is supposed to be "text 19\0this is the content"
End-goal here is to take sha-1 sum of the wholething and write the wholething to a file. I have tried some google searches, but couldn't figure out how to do it.
String wholething = header + '\0' + content;
Other ways to concatenate strings (in no particular order), each with their own particular uses:
String.concat() - Essentially the same as the + operator.
String.format() - A flexible way to build a string.
StringBuilder - Efficient way to concatenate many strings.
StringBuffer - Similar to StringBuilder, but thread-safe.
StringWriter - Extends Writer, compatible with stream IO functions.
Probably a couple more here and there.
String wholeThing = header + '\0' + content;
String wholething = header + "\0" + content;

Categories

Resources