How to auto-map attributes from Java Map to POJO class attributes - java

I am working on a spring boot application and developing a spring rest (PATCH) service.
I have a situation where I need to map the data received on the PATCH request in a HashMap from the front end to a java POJO class.
The attributes that I need to map in the POJO class are present in the key of the Hashmap. We need to iterate through the map and associate the value of the key in the map to the POJO class attribute.
What is the best way to do this? I know that there is a very leborious way where I manually check each key in the map and associate the corresponding value to the appropriate setter in the POJO class. But this is extremely cumbersome if the number of attributes in the POJO class are many in number.
Is there an efficient way to achieve this?
Code snippet
#CrossOrigin
#RequestMapping(value="/{id}", method = RequestMethod.PATCH)
public Project editProject(#RequestBody Map<String, String> project, #PathVariable("id") String id) throws Exception {
logger.info("Inside editProject() API ");
if(project == null) {
return null;
}
for(String key: project.keySet()) {
logger.info("Keys passed for update ==> Key(" + key + "): Value(" + project.get(key) + ")");
}
Project projectRec = null;
try {
logger.info("updateProject() :: Before save :: ");
projectRec = projectService.updateProjectInfo(project, id);
logger.info("updateProject() :: After save :: Saved successfully ::: ", projectRec.toString());
}
catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
logger.info("Leaving createProject() API");
return projectRec;
}
public Project updateProjectInfo(Map<String, String> projectRec, String id) {
logger.info("Inside updateProjectInfo() API in ProjectServiceImpl");
Project dbRec = projectRepository.findOne(id);
setAttributesfromMaptoDBRec(projectRec, dbRec);
Project updatedProjectRec = null;
logger.info(" Leaving updateProjectInfo() API in ProjectServiceImpl");
return updatedProjectRec;
}
private void setAttributesfromMaptoDBRec(Map<String, String> updatedProjectRecMap, Project updatedProjectRec) {
logger.info(" Inside setAttributesfromMaptoDBRec() API in ProjectServiceImpl");
Set<String> nonUpdateableAttributes = ProjectServiceImpl.nonUpdateableKeys.get(ProjectServiceImpl.nonUpdateable);
if(updatedProjectRecMap == null) {
logger.warn(" Input Map to service is null in ProjectServiceImpl ===> " + (updatedProjectRecMap == null));
return;
}
for(String key: updatedProjectRecMap.keySet()) {
logger.info(" Project property ===> " + key);
logger.info(" nonUpdateableAttributes contains key ===> " + nonUpdateableAttributes.contains(key));
if(!nonUpdateableAttributes.contains(key)) {
try {
System.out.println("Field name: " + updatedProjectRec.getClass().getField(key));
System.out.println("HOW TO AUTOMATICALLY SET THE ATTRIBUTE TO THE POJO CLASS BASED ON THE KEY ??");
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}

I have done similar thing long back using Jackson databind API.
Not sure, but this may be helpful:
import java.util.Map;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMarshalUtility {
public static <T> Map marshalObject(T obj){
ObjectMapper oMapper = new ObjectMapper();
oMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return oMapper.convertValue(obj, Map.class);
}
public static <T> T unMarshalObject(Class<T> classType, Map<String, Object> map){
ObjectMapper oMapper = new ObjectMapper();
oMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return oMapper.convertValue(map, classType);
}
}
For converting a DTO to a MAP:
Map<String,Object> searchInputMap=ObjectMarshalUtility.marshalObject(searchInputDTO);
For converting a Map to a DTO:
SearchInputDTO searchInputDTO=ObjectMarshalUtility.unMarshalObject(searchInputMap);

Related

Converting a Map<String, String> to a Value type with Jedis

So, I have a value type :
class Session {
long createdAt;
List<String> postIds;
}
Using the jedis client(3.0.0-m1 is that matters), I am currently performing an hset to create the entries and hgetAll to retrieve all the key-values:
private redis.clients.jedis.Jedis jedis;
void createSession(String idAsKey, Map<String, String> hashFieldValues) {
jedis.hset(idAsKey, hashFieldValues);
}
Map<String, String> fetchSession(String idAsKey) {
return jedis.hgetAll(idAsKey);
}
The challenge that I am currently looking at is the ease of converting the Map<String, String> into the Session object. Is there an existing way to do this?
Server response for an equivalent command
1) "createdAt"
2) "1556099708307"
3) "postIds"
4) "[a, b, c]"
PS: Starting to learn Redis, hoping this kind of mapping might have already been solved for. Yes, not looking for a client change as an answer at least.
Jedis doesn't offer a way to map objects to hash structures.
If you are using spring, then you can look at HashMappers. A HashMapper converts a POJO to a hash and vice-versa. In your case, the HashMapper will convert a Session to a hash, and the other way round.
You are not using the fields separately, but simultaneously. Because of that, I'd suggest you to use plain and simple Redis Strings instead of using Redis Hashes. So you'd be using set to save entries and get to retrieve them.
Using above suggestions, your code may become as follows:
private redis.clients.jedis.Jedis jedis;
private com.google.gson.Gson gson; // see Note
void createSession(String idAsKey, Session object) {
String serializedValue = gson.toJson(object);
jedis.set(idAsKey, serializedValue);
}
Session fetchSession(String idAsKey) {
String serializedValue = jedis.get(idAsKey);
Session deserializedObject = gson.fromJson(serializedValue, Session.class);
return deserializedObject;
}
Note: I have used Gson for the purpose of serialization/deserialization. Needless to say, you can use any library.
You can convert the map to POJO
Session session = new ObjectMapper().convertValue(map, Session.class);
So you don't need a special handling expect using a mapper library as Jackson-Databind
You can save and fetch the data to and from Redis like below:
public Map<String, Object> saveDataInRedis(String id, Object obj) {
Map<String, Object> result = new HashMap<>();
String jsonObj = "";
try {
jsonObj = objectMapper.writeValueAsString(obj);
System.out.println(jsonObj);
} catch (JsonProcessingException jpe) {
logger.warn("In saveDataInRedis Exception :: "+jpe);
}
try {
valOps.set(id, jsonObj);
result.put(DataConstants.IS_SUCCESS, true);
result.put(DataConstants.MESSAGE, "Data saved succesfully in redis");
}catch(RedisConnectionFailureException e){
result =null;
logger.warn("In saveDataInRedis Exception e :: "+e);
}
System.out.println(valOps.getOperations().getClass());
System.out.println(jedisConnectionFactory.getPoolConfig().getMaxTotal());
return result;
}
Now get data from redis:
public Map<String, Object> getDataFromRedis(String id) {
Map<String, Object> result = new HashMap<>();
String jsonObj = valOps.get(id);
System.out.println("jsonObj :: " + jsonObj);
Session obj = null;
try {
obj = (Session) objectMapper.readValue(jsonObj, Session.class);
} catch (Exception e) {
result.put("data", null);
logger.warn("Data from redis is deleted");
logger.warn("In getDataFromRedis Exception e :: "+e);
}
if (obj != null) {
result.put(DataConstants.IS_SUCCESS, true);
result.put("data", obj);
}
System.out.println("result :: " + result);
return result;
}

DynamoDB - how to get Primary Key (which is random id) from database to make endpoind class?

I've made method that I use to edit Item from database.
This is how my method looks:
public Product editProduct(PrimaryKey primaryKey, Product content) {
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withValueMap(createValueMap(content));
UpdateItemOutcome itemOutcome = databaseController.getTable(PRODUCT_TABLE).updateItem(updateItemSpec);
return convertToProduct(itemOutcome);
}
private Map<String, Object> createValueMap(Product content) {
Map<String, Object> result = new HashMap<>();
result.put("name", content.getName());
result.put("calories", content.getCalories());
result.put("fat", content.getFat());
result.put("carbo", content.getCarbo());
result.put("protein", content.getProtein());
result.put("productKinds", content.getProductKinds());
result.put("author", content.getAuthor());
result.put("media", content.getMedia());
result.put("approved", content.getApproved());
return result;
}
private Product convertToProduct(UpdateItemOutcome itemOutcome) {
Product product = new Product();
product.setName(itemOutcome.getItem().get("name").toString());
product.setCalories(itemOutcome.getItem().getInt("calories"));
product.setFat(itemOutcome.getItem().getDouble("fat"));
product.setCarbo(itemOutcome.getItem().getDouble("carbo"));
product.setProtein(itemOutcome.getItem().getDouble("protein"));
product.setProductKinds(itemOutcome.getItem().getList("productKinds"));
ObjectMapper objectMapper = new ObjectMapper();
try {
Author productAuthor = objectMapper.readValue(itemOutcome.getItem().getString("author"), Author.class);
product.setAuthor(productAuthor);
} catch (IOException e) {
e.printStackTrace();
}
try {
Media productMedia = objectMapper.readValue(itemOutcome.getItem().getString("media"), Media.class);
product.setMedia(productMedia);
} catch (IOException e) {
e.printStackTrace();
}
return product;
}
Now I want to create endpoint class for this method but I have problem, I need to get primarykey as parameter (it's looks like this for example: 2567763a-d21e-4146-8d61-9d52c2561fc0) and I don't know how to do this.
At the moment my class looks like that:
public class EditProductLambda implements RequestHandler<Map<String, Object>, ApiGatewayResponse> {
private LambdaLogger logger;
#Override
public ApiGatewayResponse handleRequest(Map<String, Object> input, Context context) {
logger = context.getLogger();
logger.log(input.toString());
try{
Product product = RequestUtil.parseRequest(input, Product.class);
//PrimaryKey primaryKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
KitchenService kitchenService = new KitchenService(new DatabaseController(context, Regions.EU_CENTRAL_1), logger);
Product editedProduct = kitchenService.editProduct(primaryKey, product);
return ResponseUtil.generateResponse(HttpStatus.SC_CREATED, editedProduct);
} catch (IllegalArgumentException e){
return ResponseUtil.generateResponse(HttpStatus.SC_BAD_REQUEST, e.getMessage());
}
}
Can someone give me some advice how to do that? Or maybe my method is done wrong?
So first you have to create a trigger to Lambda function and ideal prefer here would be an API gateway. You can pass your data as query string or as a request body to API gateway.
You can use body mapping template in the integration request section of API gateway and get request body/query string. Construct a new json at body mapping template, which will have data from request body/query string. As we are adding body mapping template your business logic will get the json we have constructed at body mapping template.
Inside body mapping template to get query string please do ,
$input.params('querystringkey')
For example inside body mapping template (If using query string),
#set($inputRoot = $input.path('$'))
{
"primaryKey" : "$input.params('$.primaryKey')"
}
if passing data as body then,
#set($inputRoot = $input.path('$'))
{
"primaryKey" : "$input.path('$.primaryKey')"
}
Please read https://aws.amazon.com/blogs/compute/tag/mapping-templates/ for more details on body mapping template

How can I update custom properties in alfresco workflow task using only Java?

First, I want to say thanks to everyone that took their time to help me figure this out because I was searching for more than a week for a solution to my problem. Here it is:
My goal is to start a custom workflow in Alfresco Community 5.2 and to set some custom properties in the first task trough a web script using only the Public Java API. My class is extending AbstractWebScript. Currently I have success with starting the workflow and setting properties like bpm:workflowDescription, but I'm not able to set my custom properties in the tasks.
Here is the code:
public class StartWorkflow extends AbstractWebScript {
/**
* The Alfresco Service Registry that gives access to all public content services in Alfresco.
*/
private ServiceRegistry serviceRegistry;
public void setServiceRegistry(ServiceRegistry serviceRegistry) {
this.serviceRegistry = serviceRegistry;
}
#Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
// Create JSON object for the response
JSONObject obj = new JSONObject();
try {
// Check if parameter defName is present in the request
String wfDefFromReq = req.getParameter("defName");
if (wfDefFromReq == null) {
obj.put("resultCode", "1 (Error)");
obj.put("errorMessage", "Parameter defName not found.");
return;
}
// Get the WFL Service
WorkflowService workflowService = serviceRegistry.getWorkflowService();
// Build WFL Definition name
String wfDefName = "activiti$" + wfDefFromReq;
// Get WorkflowDefinition object
WorkflowDefinition wfDef = workflowService.getDefinitionByName(wfDefName);
// Check if such WorkflowDefinition exists
if (wfDef == null) {
obj.put("resultCode", "1 (Error)");
obj.put("errorMessage", "No workflow definition found for defName = " + wfDefName);
return;
}
// Get parameters from the request
Content reqContent = req.getContent();
if (reqContent == null) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing request body.");
}
String content;
content = reqContent.getContent();
if (content.isEmpty()) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Content is empty");
}
JSONTokener jsonTokener = new JSONTokener(content);
JSONObject json = new JSONObject(jsonTokener);
// Set the workflow description
Map<QName, Serializable> params = new HashMap();
params.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION, "Workflow started from JAVA API");
// Start the workflow
WorkflowPath wfPath = workflowService.startWorkflow(wfDef.getId(), params);
// Get params from the POST request
Map<QName, Serializable> reqParams = new HashMap();
Iterator<String> i = json.keys();
while (i.hasNext()) {
String paramName = i.next();
QName qName = QName.createQName(paramName);
String value = json.getString(qName.getLocalName());
reqParams.put(qName, value);
}
// Try to update the task properties
// Get the next active task which contains the properties to update
WorkflowTask wfTask = workflowService.getTasksForWorkflowPath(wfPath.getId()).get(0);
// Update properties
WorkflowTask updatedTask = workflowService.updateTask(wfTask.getId(), reqParams, null, null);
obj.put("resultCode", "0 (Success)");
obj.put("workflowId", wfPath.getId());
} catch (JSONException e) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
e.getLocalizedMessage());
} catch (IOException ioe) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Error when parsing the request.",
ioe);
} finally {
// build a JSON string and send it back
String jsonString = obj.toString();
res.getWriter().write(jsonString);
}
}
}
Here is how I call the webscript:
curl -v -uadmin:admin -X POST -d #postParams.json localhost:8080/alfresco/s/workflow/startJava?defName=nameOfTheWFLDefinition -H "Content-Type:application/json"
In postParams.json file I have the required pairs for property/value which I need to update:
{
"cmprop:propOne" : "Value 1",
"cmprop:propTwo" : "Value 2",
"cmprop:propThree" : "Value 3"
}
The workflow is started, bpm:workflowDescription is set correctly, but the properties in the task are not visible to be set.
I made a JS script which I call when the workflow is started:
execution.setVariable('bpm_workflowDescription', 'Some String ' + execution.getVariable('cmprop:propOne'));
And actually the value for cmprop:propOne is used and the description is properly updated - which means that those properties are updated somewhere (on execution level maybe?) but I cannot figure out why they are not visible when I open the task.
I had success with starting the workflow and updating the properties using the JavaScript API with:
if (wfdef) {
// Get the params
wfparams = {};
if (jsonRequest) {
for ( var prop in jsonRequest) {
wfparams[prop] = jsonRequest[prop];
}
}
wfpackage = workflow.createPackage();
wfpath = wfdef.startWorkflow(wfpackage, wfparams);
The problem is that I only want to use the public Java API, please help.
Thanks!
Do you set your variables locally in your tasks? From what I see, it seems that you define your variables at the execution level, but not at the state level. If you take a look at the ootb adhoc.bpmn20.xml file (https://github.com/Activiti/Activiti-Designer/blob/master/org.activiti.designer.eclipse/src/main/resources/templates/adhoc.bpmn20.xml), you can notice an event listener that sets the variable locally:
<extensionElements>
<activiti:taskListener event="create" class="org.alfresco.repo.workflow.activiti.tasklistener.ScriptTaskListener">
<activiti:field name="script">
<activiti:string>
if (typeof bpm_workflowDueDate != 'undefined') task.setVariableLocal('bpm_dueDate', bpm_workflowDueDate);
if (typeof bpm_workflowPriority != 'undefined') task.priority = bpm_workflowPriority;
</activiti:string>
</activiti:field>
</activiti:taskListener>
</extensionElements>
Usually, I just try to import all tasks for my custom model prefix. So for you, it should look like that:
import java.util.Set;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.apache.log4j.Logger;
public class ImportVariables extends AbstractTaskListener {
private Logger logger = Logger.getLogger(ImportVariables.class);
#Override
public void notify(DelegateTask task) {
logger.debug("Inside ImportVariables.notify()");
logger.debug("Task ID:" + task.getId());
logger.debug("Task name:" + task.getName());
logger.debug("Task proc ID:" + task.getProcessInstanceId());
logger.debug("Task def key:" + task.getTaskDefinitionKey());
DelegateExecution execution = task.getExecution();
Set<String> executionVariables = execution.getVariableNamesLocal();
for (String variableName : executionVariables) {
// If the variable starts by "cmprop_"
if (variableName.startsWith("cmprop_")) {
// Publish it at the task level
task.setVariableLocal(variableName, execution.getVariableLocal(variableName));
}
}
}
}

How to know Gigaspace is connected in Java application at startup

I am using spring Application and my gigaspace is connecting at startup. I am not getting any exception, if gigaspace is down.
#Override
public void onContextRefreshed(ContextRefreshedEvent event) {
String gigaSpaceURL = null;
LOGGER.info("({}) initializing gigaspaces client", getName());
try {
initGSProxy();
Iterator<Map.Entry<ConfiguredSpace, Space>> entries = spaces.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<ConfiguredSpace, Space> entry = entries.next();
LOGGER.info("({}) initialing space- key=" +
entry.getKey() + ", value = " + entry.getValue(),
getName());
// TODO : Need to verify Boolean Value Input
gigaspace.createSpace(entry.getKey().name(),
entry.getValue().getURL(), false);
gigaSpaceURL = entry.getValue().getURL();
}
} catch (Exception e) {
return;
}
GenericUtil.updateLogLevel("INFO",
"com.renovite.ripps.ap.gs.Spaces");
LOGGER.info("\n************************************\nConnected with Gigaspace successfully:URL:" + gigaSpaceURL
+ "\n************************************\n");
GenericUtil.updateLogLevel("ERROR",
"com.renovite.ripps.ap.gs.Spaces");
}
Take reference of Gigaspace by using getGigaSpace() method which takes spacekey as an argument.If it throw exception at run time, it means application is not able to connect with specified Gigaspace url.
Or more elegant way, In your Gigaspace proxy class (which actually implements IGigaspace) override the getGigaSpace() method such that it will return null if connection is not possible.
/** The spaces. */
private transient Map spaces = new HashMap<>();
#Override
public GigaSpace getGigaSpace(String spaceKey) {
if(spaces.get(spaceKey) != null){
return spaces.get(spaceKey).getGigaSpace();
}
return null;
}
spaces is a Map of all urls that are registered with Gigapsace.If no one is registered, we are returning null in the above method.

Remove duplicates from a Json String in Java?

I have a Json String with duplicate values:
String json = "{\"Sign_In_Type\":\"Action\",\"Sign_In_Type\":\"Action\"}";
that correctly throws an exception when I try to create a JSONObject:
try {
JSONObject json_obj = new JSONObject(json);
String type = json_obj.getString("Sign_In_Type");
} catch (JSONException e) {
throw new RuntimeException(e);
}
Error:
Exception in thread "main" java.lang.RuntimeException: org.json.JSONException: Duplicate key "Sign_In_Type"
at com.campanja.app.Upload.main(Upload.java:52)
Caused by: org.json.JSONException: Duplicate key "Sign_In_Type"
at org.json.JSONObject.putOnce(JSONObject.java:1076)
at org.json.JSONObject.(JSONObject.java:205)
at org.json.JSONObject.(JSONObject.java:402)
at com.campanja.app.Upload.main(Upload.java:49)
Is there a smart way of removing or checking for duplicates before I convert it to a JSONOBject?
I have tried to create:
Set set = new HashSet(Arrays.asList(json));
but that gives me:
[{"Sign_In_Type":"Action","Sign_In_Type":"Action"}]
Any suggesstions welcome, thanks!
Two options I can think of right off the bat:
Parse the string using wither regex or tokens, add each key-value pair to a hashmap, and in the end recreate your JSON document with the duplicates removed. In this case though I would only remove key-value pairs that are exactly the same.
Download the source code for org.json.JSONObject , and make a slight modification to the code to automatically leave out duplicates. This is a bit dangerous though. Another option is to create a modified version that simply validates and modifies.
Extending JSONObject Working Example
The below code allows you to create a JSONOBbject with a string containing duplicate keys. Exceptions are thrown only when you have two key-values that have the same key, but different values. This was because I think it would be a problem to choose at random which of the two should be assigned (e.g. the later value?). Of course this can be changed to work as you wish (e.g. keep last value for multiple keys).
Modified Class
import org.json.JSONException;
import org.json.JSONObject;
public class JSONObjectIgnoreDuplicates extends JSONObject {
public JSONObjectIgnoreDuplicates(String json) {
super(json);
}
public JSONObject putOnce(String key, Object value) throws JSONException {
Object storedValue;
if (key != null && value != null) {
if ((storedValue = this.opt(key)) != null ) {
if(!storedValue.equals(value)) //Only through Exception for different values with same key
throw new JSONException("Duplicate key \"" + key + "\"");
else
return this;
}
this.put(key, value);
}
return this;
}
}
Main method
String json = "{\"Sign_In_Type\":\"Action\",\"Sign_In_Type\":\"Action\"}";
try {
JSONObject json_obj = new JSONObjectIgnoreDuplicates(json);
String type = json_obj.getString("Sign_In_Type");
} catch (JSONException e) {
throw new RuntimeException(e);
}
Assuming that
String json = "{\"Sign_In_Type\":\"Action\",\"Sign_In_Type\":\"Action\"}";
is a fiction for testing, can I ask whether creating the data as a String is the best choice in the first place? Why not a HashMap, or some other structure that either overwrites the subsequent reuses of a name or ignores them or throws an error when you add them? Don't wait until the conversion to JSON to make your data valid.
You can make use of the Jackson library to parse JSON. I'd problems doing the same task as you with org.json's package, but I turned to Jackson and I solved it: http://wiki.fasterxml.com/JacksonHome
I expanded Menelaos Bakopoulos answer, so that if inner values are also with duplicates, it won't create issues. the former solution worked on the first level only.
public class JSONObjectIgnoreDuplicates extends JSONObject {
public JSONObjectIgnoreDuplicates(JSONTokener x) throws JSONException {
super(x);
}
#Override
public JSONObject putOnce(String key, Object value) throws JSONException {
Object storedValue;
if (key != null && value != null) {
if ((storedValue = this.opt(key)) != null) {
if (!storedValue.toString().equals(value.toString())) //Only throw Exception for different values with same key
throw new JSONException("Duplicate key \"" + key + "\"");
else
return this;
}
this.put(key, value);
}
return this;
}
}
private class JsonDupTokener extends JSONTokener {
public JsonDupTokener(String s) {
super(s);
}
#Override
public Object nextValue() throws JSONException {
char c = this.nextClean();
switch (c) {
case '\"':
case '\'':
return this.nextString(c);
case '[':
this.back();
return new JSONArray(this);
case '{':
this.back();
return new JSONObjectIgnoreDuplicates(this);
default:
StringBuffer sb;
for (sb = new StringBuffer(); c >= 32 && ",:]}/\\\"[{;=#".indexOf(c) < 0; c = this.next()) {
sb.append(c);
}
this.back();
String string = sb.toString().trim();
if ("".equals(string)) {
throw this.syntaxError("Missing value");
} else {
return JSONObject.stringToValue(string);
}
}
}
}
Sorry I can't comment on Menelaos Bakopoulos' response due to reputation<50... Stupid system
Your solution unfortunately does not work here:
SEVERE: ERROR converting JSON to XML org.json.JSONException: Duplicate key "id"
org.json.JSONObject.putOnce(JSONObject.java:1076)
org.json.JSONObject.<init>(JSONObject.java:205)
org.json.JSONTokener.nextValue(JSONTokener.java:344)
org.json.JSONArray.<init>(JSONArray.java:125)
org.json.JSONTokener.nextValue(JSONTokener.java:348)
org.json.JSONObject.<init>(JSONObject.java:205)
JSONUtilities.JSONObjectIgnoreDuplicates.<init>(JSONUtilities.java:38)
It seems that calling super(json) in JSONObjectIgnoreDuplicates's constructor sends the code into a loop inside JSONObject, not JSONObjectIgnoreDuplicates ;{
I'm currently trying Asaf Bartov's solution, but there's no call from JSONObjectIgnoreDuplicates to JsonDupTokener, so appart from overloading the constructor of JSONObjectIgnoreDuplicates as follows, I don't see how it could work:
public JSONObjectIgnoreDuplicates(String json) throws JSONException {
this(new JSONDupTokener(json));
}
EDIT: I can confirm this works :))))
Thanks everybody!!!!
With Google Gson you can decide what to do with duplicates in the input string. You need to register your own TypeAdapter responsible for serialization/deserialization of objects. It would look like this:
// this implementation converts the json string to a Map<String, String>,
// saving only the first duplicate key and dropping the rest
class NoDuplicatesAdapter extends TypeAdapter<HashMap<String, String>> {
#Override
public void write(JsonWriter out, HashMap<String, String> value) throws IOException {
out.beginObject();
for (Map.Entry<String, String> e: value.entrySet()) {
out.name(e.getKey()).value(e.getValue());
}
out.endObject();
}
#Override
public HashMap<String, String> read(JsonReader in) throws IOException {
final HashMap<String, String> map = new HashMap<>();
in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
// putting value to the map only if this key is not present;
// here you can actually find duplicate keys and decide what to do with them
map.putIfAbsent(name, in.nextString());
}
in.endObject();
return map;
}
}
Then you can parse your string:
String json = "{\"Sign_In_Type\":\"Action\",\"Sign_In_Type\":\"Action\"}";
Type mapType = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> map = new GsonBuilder()
.registerTypeAdapter(mapType, new NoDuplicatesAdapter())
.create()
.fromJson(str, mapType);
The map will contain only the first "Sign_In_Type".

Categories