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
Why deepCopy is not public? #760
Comments
It was never made public in a released version. No specific reason, we just didn't see it useful enough to bloat our API. What are you trying to do that you need it? |
Fair enough, I was only checking the file history and didn't notice that, sorry.
To be exact for this but a shorter version is: class Demo {
private JsonObject serverResponse;
public JsonObject getResultsFromResponse() {
JsonObject results = serverResponse.getAsJsonObject("results");
results.add("SOME_DEFAULT_DATA", "");
return results;
}
} In this code piece calling Let me know if I overlooked something or if this is possible to do without copying. |
We could support deep copy by exposing clone() with a covariant return type On Sat, Dec 26, 2015, 12:36 PM Cihat Keser notifications@github.com wrote:
|
Two things:
|
And for what it’s worth, a shallow copy is sufficient to satisfy @kramer’s request. |
@swankjesse May be true for the exact code I posted but are you sure that a shallow copy is still sufficient if the modification (the field addition) was in a deeper level (e.g.: |
Yep, in that case you’ll need a deep copy. |
I came across this thread when I encountered the fact that I cannot extend JsonElement because of the default access level of the deepCopy. Is there any alternative for me? I want to implement my own class which extends JsonElement. |
Hi @kramer, so far have you found any solution to solve the issue? I'm right facing the same issue as you did.. |
Hi, I'm looking at ways to merge Jsons the way its available in lodash. Regards, |
No specific reason to hide deepCopy (except to have a smaller API set). |
@inder123 now i did the deepCopy in this way for Gson
So far it works for what I need, but I'm not sure whether it is good in term of process performance. |
+1 for making deepCopy() public. My code below would alter the source, which I'd like to prevent:
|
+1 deepCopy() should be public, I would use it when merging two JsonObject's into one when the JsonObjects are settings and default settings... like so...
|
+1 to make the deepCopy public. This feature is naturally expected to be provided by the gson library. There are lot of cases where you just want to take some your JsonObject as a source (or template) for your further manipulations(adding/removing properties, etc) but keeping the original untouched. with deepCopy you don't need to reinvent a wheel in every of your projects. you simply use it out of the box. |
Guys please make deepCopy public... |
yes, it needs a way to clone. I prefer just implements Cloneable interface and just make it deepCopy. |
I am open to it. Please send a pull request. Thanks.
…On Thu, Nov 24, 2016 at 4:46 PM, Edgenius ***@***.***> wrote:
yes, it needs a way to clone. I prefer just implements Cloneable interface
and just make it deepCopy.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#760 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABCwyo9L5cUFXRmWwiM-EYOqVD1NcSHXks5rBi__gaJpZM4G7cBz>
.
|
+1 |
+1 make it public |
Do it already... |
+1 need public |
+1 My life would be easier if this were public. Here's my workaround solution: public class GsonUtils {
public static JsonObject deepCopy(JsonObject jsonObject) {
JsonObject result = new JsonObject();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
if (entry.getValue().isJsonObject()) {
result.add(entry.getKey(), deepCopy(entry.getValue().getAsJsonObject()));
} else if (entry.getValue().isJsonArray()) {
result.add(entry.getKey(), deepCopy(entry.getValue().getAsJsonArray()));
} else if (entry.getValue().isJsonPrimitive()) {
result.add(entry.getKey(), new JsonPrimitive(entry.getValue().getAsString()));
}
}
return result;
}
private static JsonArray deepCopy(JsonArray jsonArray) {
JsonArray result = new JsonArray();
for (JsonElement jsonElement : jsonArray) {
if (jsonElement.isJsonObject()) {
result.add(deepCopy(jsonElement.getAsJsonObject()));
} else if (jsonElement.isJsonArray()) {
result.add(deepCopy(jsonElement.getAsJsonArray()));
} else if (jsonElement.isJsonPrimitive()) {
result.add(new JsonPrimitive(jsonElement.getAsString()));
}
}
return result;
}
} |
very useful function, will wait for it |
It's public now, please close. |
@eamonnmcmanus, this was resolved by #1091. |
Is there a reason why the
deepCopy
methods are not public? I see that they were public for some time in an earlier revision then was made "default" access level but I couldn't find the reason.The text was updated successfully, but these errors were encountered: