Reputation: 68
I have a case wherein I have the jsonpointer and the swagger json file. I need to get the line number to which the jsonpointer is pointing to.
Is there a way to do so, I used ObjectMapper, I get the jsonNode, I also saw in the the code, the JsonParser has the information about the line number when the json file is parsed, I there a way to get the line number for each ObjectNode, or just pass the jsonPointer and get the line number.
Below is the code:
public class JsonPointerUtil {
static ObjectMapper om = new ObjectMapper();
private JsonNode jsonNode;
public JsonPointerUtil(File json) throws IOException {
jsonNode = com.xyz.util.JsonPointerUtil.om.readTree(json);
}
public String getJsonBlockForPointer(String jsonPtr) {
if (StringUtils.isEmpty(jsonPtr))
return null;
JsonNode at = jsonNode.at(jsonPtr);
return at.toString();
}
}
So in getJsonBlockForPointer method using the pointer I get the json node, The json node is the exact json block pointed by the pointer, I need the line number also of the pointed json block, Is there a way I can get that.
My requirement is, the line number is used by UI side code, there are multiple UI apps which call the same backend service, On UI side, the jsonPointer library gives me all the information (line,col no, agiainst each jsonPointer), but as there are multiple UI apps consuming the same service, I want to send all theinfo from backend.
Upvotes: 0
Views: 687
Reputation: 68
posting after long time, we ultimately ended up using javascript only, we called the javascript from java and passing the relevant details.
we basically used
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
Invocable inv = (Invocable) engine;
long start = new Date().getTime();
Gson gson=new Gson();
parse = inv.invokeFunction("getLineNumbers", jsonPointers, swaggerAsString);
called the javascript file from java.
Upvotes: 0