Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality to copy data from a Map and put them into an already initialized JSONObject #528

Closed
viveksacademia4git opened this issue Jun 6, 2020 · 4 comments

Comments

@viveksacademia4git
Copy link
Contributor

viveksacademia4git commented Jun 6, 2020

I have a use case wherein I want to copy all the data (key-value pairs) of a java.util.Map object and put them into a JSONObject, which is already initialized or maybe it even contains some data. I am doing this task in the following manner:

// jsonObject already containing some data
...
     if (map != null && !map.isEmpty()) {
        for (final Entry<?, ?> e : map.entrySet()) {
            final Object key = e.getKey();
    	    if (key == null) {
    	        continue;
    	    }
            final Object value = e.getValue();
            if (value != null) {
                jsonObject.put(String.valueOf(key), value);
            }
        }
     }
...

What I am looking for is a method within the JSONObject class that perform the above task for me; like in the following code:

...
public class JSONObject {
...
    public void put(Map<?,?> map) {
    	if (map != null && !map.isEmpty()) {
	    	for (final Entry<?, ?> e : map.entrySet()) {
	            final Object key = e.getKey();
	    	    if (key == null) {
	    	        continue;
	    	    }
	            final Object value = e.getValue();
	            if (value != null) {
	                this.map.put(String.valueOf(key), wrap(value));
	            }
	        }
    	}
    }
}
...
viveksacademia4git pushed a commit to viveksacademia4git/JSON-java that referenced this issue Jun 6, 2020
- Method in JSONObject is put(Map)
- Also written unit-tests in JSONObjectTest.java for this new function
viveksacademia4git pushed a commit to viveksacademia4git/JSON-java that referenced this issue Jun 6, 2020
@stleary
Copy link
Owner

stleary commented Jun 7, 2020

Let's try to limit new APIs to essential functionality. We already have a JSONObject constructor that takes a map parameter.

@johnjaylward
Copy link
Contributor

I'm not sure this fits in at a library level for this library. This sounds more like a Map Merge function, which would probably go in a more generic collections utility.

@viveksacademia4git
Copy link
Contributor Author

viveksacademia4git commented Jun 12, 2020

@stleary, Of course, JSONObject constructor performs the task of copying data from Map to the JSONObject during initialization but this use case is different focusing on an already initialized JSONObject.
My concern is that a lot of programmers like myself have to come up with their own utility function or write the program with a loop every time, for this use case. Having this functionality could decrease the code size, save time, and also avoid issues, whenever writing the program for this use case.

Anyways, I have become aware of the priority for this library now.
I will be closing this issue now.

Thank you.

@johnjaylward
Copy link
Contributor

johnjaylward commented Jul 21, 2020

In regards to #529 and it's relation to this PR, I still feel that this method as implemented is more application specific than library. When dealing with map merges like this one there are multiple cases to consider if the key already exists in both maps.

One way of handling it may be to consider implementing multiple "putAll" methods:

  1. accumulateAll(Map<?,?> map) which would go through each key in the map, and use the JSONObject.accumulate. You would need to verify that if the received map's key references an array, that accumulate appends the values in the array, not the array itself.
  2. putAllOnce(Map<?,?> map) which would go through each key, and if the key already exists in the target JSONObject, would skip it
  3. putAll(Map<?,?> map) which would go through each key, and if the key already exists in the target JSONObject, throw a JSONException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants