Skip to content
Advertisement

How to avoid escaping /n on each microservice in the chain? Java

For example, we are getting a json message like this from our partner:

{
"message": "Dear client,nWe'd like to offer you.."
}

The partner wants the client to receive the message like this (without newline but with n)

Dear client,nWe'd like to offer you

But we have a chain of microservices in our ecosystem and that json goes through 4 or 5 microservices which proccesing it before client can get it. So, our partner should give us \\\\n instead of n in order to client got n in the result. But I’m wondering, is adding 8 backslashes in the source message to escape “n” for every microservice the only way to solve this problem. I think it’s not really good solution, because we have to make changes in source message if count of microservices in chain changes (Moreover, we will face he problem if count of microservices in the chain start changing dynamically)? Is there way to use n in source message (from partner) without replacing every n with \n in our microservies?

There is an example how I process the json in one of the microservices:

private String replace(String sourceJson, List<String> properties, DocumentContext context) {
    StringBuilder stringBuilder = new StringBuilder(sourceJson);

    for (String property : properties) {
        String newValue = Pattern.compile("ABC")
                .matcher(stringBuilder)
                .replaceAll(context.read(property, String.class));
        stringBuilder.replace(0, stringBuilder.length(), newValue);
    }
    return stringBuilder.toString();
}

Advertisement

Answer

Here’s an example of creating a JSON object, turning it into a String and and then back into a JsonObject, adding a property, and turning it back into a String again.

package org.example;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

public class App {
    public static void main(String[] args) {
        JsonObject o = new JsonObject();
        o.addProperty("message","foonbar");
        Gson gson = new Gson();
        String stringRep = gson.toJson(o);
        System.out.println(stringRep);
        JsonObject o2 = gson.fromJson(stringRep, JsonObject.class);
        o2.addProperty("newProp", 42);
        String messageValue = o2.get("message").getAsString();
        System.out.println(messageValue);
        String newMessageValue = messageValue.replace("foo", "baz");
        o2.addProperty("message", newMessageValue);
        stringRep = gson.toJson(o2);
        System.out.println(stringRep);
    }
}

The output of this program is:

{"message":"foonbar"}
foo
bar
{"message":"baznbar","newProp":42}

So you can see the the Java representations of strings contain newline characters, but the JSON representations contain the character sequence '', 'n'.

The maven dependency you need is:

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.9</version>
    </dependency>

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement