How to parse a specified class which extends a base class using Gson?
Let's first state the (base) classes that are part of this:
// the data class. This contains our values
public class Data<T extends Annotation> {
public transient final T info; // gson ignores transients
public Data(Class<T> annotation) { info = getClass().getAnnotation(annotation); }
}
// the manager class, this contains numerous Data's
public class Manager<T> {
protected final ArrayList<T> collection = new ArrayList<T>();
public Collection<T> GetCollection() { return collection; }
}
Now suppose I've implemented the above classes in the following Manner:
// my implemented manager. It contains a bunch of MyBase objects
public class MyManager extends Manager<MyBase> {
public MyManager() {
collection.add(new MyCustomBase());
// some more...
}
}
public class MyBase extends MyData {
// some stuff for my base, such as various methods for calculating and rendering
}
public MyData extends Data<MyData.MyInterface> {
public String foo;
MyData() {
super(MyData.MyInterface.class);
foo = info.foo(); // handle our interface variables, since interfaces cannot be directly modified
}
@Retention
public @interface MyInterface {
String foo();
}
For sake of hopefully understandable code, my custom base looks like this:
@MyData.MyInterface(foo = "bar")
public class CustomBase extends MyBase {
// other various methods for rendering and calculating, some events in here too
}
Now, in GSON, I'd like to print out the information from MyData. This is the way I've gone about trying to do it:
public class Config {
// store the 'Managers' which we want to save the data of inside of an arrayList, and then iterate through them and their contents for 'Data', then just save it.
//...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
PrintWriter writer = new PrintWriter(myJSON);
for (Manager m : managerList) {
for (Object o : m.GetCollection()) {
Data d = (Data) o;
// expected result, a json with "foo": bar
writer.println(gson.toJson(d)); // this doesn't work, throws a stack overflow
}
writer.close();
}
Unfortunately, this approach is not working. It, for some weird reason, throws a Stack Overflow. What might be a better approach for this?
Edit: I won't handle the interface directly since foo, or any other values will change during the course of the program.
java gson config
add a comment |
Let's first state the (base) classes that are part of this:
// the data class. This contains our values
public class Data<T extends Annotation> {
public transient final T info; // gson ignores transients
public Data(Class<T> annotation) { info = getClass().getAnnotation(annotation); }
}
// the manager class, this contains numerous Data's
public class Manager<T> {
protected final ArrayList<T> collection = new ArrayList<T>();
public Collection<T> GetCollection() { return collection; }
}
Now suppose I've implemented the above classes in the following Manner:
// my implemented manager. It contains a bunch of MyBase objects
public class MyManager extends Manager<MyBase> {
public MyManager() {
collection.add(new MyCustomBase());
// some more...
}
}
public class MyBase extends MyData {
// some stuff for my base, such as various methods for calculating and rendering
}
public MyData extends Data<MyData.MyInterface> {
public String foo;
MyData() {
super(MyData.MyInterface.class);
foo = info.foo(); // handle our interface variables, since interfaces cannot be directly modified
}
@Retention
public @interface MyInterface {
String foo();
}
For sake of hopefully understandable code, my custom base looks like this:
@MyData.MyInterface(foo = "bar")
public class CustomBase extends MyBase {
// other various methods for rendering and calculating, some events in here too
}
Now, in GSON, I'd like to print out the information from MyData. This is the way I've gone about trying to do it:
public class Config {
// store the 'Managers' which we want to save the data of inside of an arrayList, and then iterate through them and their contents for 'Data', then just save it.
//...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
PrintWriter writer = new PrintWriter(myJSON);
for (Manager m : managerList) {
for (Object o : m.GetCollection()) {
Data d = (Data) o;
// expected result, a json with "foo": bar
writer.println(gson.toJson(d)); // this doesn't work, throws a stack overflow
}
writer.close();
}
Unfortunately, this approach is not working. It, for some weird reason, throws a Stack Overflow. What might be a better approach for this?
Edit: I won't handle the interface directly since foo, or any other values will change during the course of the program.
java gson config
add a comment |
Let's first state the (base) classes that are part of this:
// the data class. This contains our values
public class Data<T extends Annotation> {
public transient final T info; // gson ignores transients
public Data(Class<T> annotation) { info = getClass().getAnnotation(annotation); }
}
// the manager class, this contains numerous Data's
public class Manager<T> {
protected final ArrayList<T> collection = new ArrayList<T>();
public Collection<T> GetCollection() { return collection; }
}
Now suppose I've implemented the above classes in the following Manner:
// my implemented manager. It contains a bunch of MyBase objects
public class MyManager extends Manager<MyBase> {
public MyManager() {
collection.add(new MyCustomBase());
// some more...
}
}
public class MyBase extends MyData {
// some stuff for my base, such as various methods for calculating and rendering
}
public MyData extends Data<MyData.MyInterface> {
public String foo;
MyData() {
super(MyData.MyInterface.class);
foo = info.foo(); // handle our interface variables, since interfaces cannot be directly modified
}
@Retention
public @interface MyInterface {
String foo();
}
For sake of hopefully understandable code, my custom base looks like this:
@MyData.MyInterface(foo = "bar")
public class CustomBase extends MyBase {
// other various methods for rendering and calculating, some events in here too
}
Now, in GSON, I'd like to print out the information from MyData. This is the way I've gone about trying to do it:
public class Config {
// store the 'Managers' which we want to save the data of inside of an arrayList, and then iterate through them and their contents for 'Data', then just save it.
//...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
PrintWriter writer = new PrintWriter(myJSON);
for (Manager m : managerList) {
for (Object o : m.GetCollection()) {
Data d = (Data) o;
// expected result, a json with "foo": bar
writer.println(gson.toJson(d)); // this doesn't work, throws a stack overflow
}
writer.close();
}
Unfortunately, this approach is not working. It, for some weird reason, throws a Stack Overflow. What might be a better approach for this?
Edit: I won't handle the interface directly since foo, or any other values will change during the course of the program.
java gson config
Let's first state the (base) classes that are part of this:
// the data class. This contains our values
public class Data<T extends Annotation> {
public transient final T info; // gson ignores transients
public Data(Class<T> annotation) { info = getClass().getAnnotation(annotation); }
}
// the manager class, this contains numerous Data's
public class Manager<T> {
protected final ArrayList<T> collection = new ArrayList<T>();
public Collection<T> GetCollection() { return collection; }
}
Now suppose I've implemented the above classes in the following Manner:
// my implemented manager. It contains a bunch of MyBase objects
public class MyManager extends Manager<MyBase> {
public MyManager() {
collection.add(new MyCustomBase());
// some more...
}
}
public class MyBase extends MyData {
// some stuff for my base, such as various methods for calculating and rendering
}
public MyData extends Data<MyData.MyInterface> {
public String foo;
MyData() {
super(MyData.MyInterface.class);
foo = info.foo(); // handle our interface variables, since interfaces cannot be directly modified
}
@Retention
public @interface MyInterface {
String foo();
}
For sake of hopefully understandable code, my custom base looks like this:
@MyData.MyInterface(foo = "bar")
public class CustomBase extends MyBase {
// other various methods for rendering and calculating, some events in here too
}
Now, in GSON, I'd like to print out the information from MyData. This is the way I've gone about trying to do it:
public class Config {
// store the 'Managers' which we want to save the data of inside of an arrayList, and then iterate through them and their contents for 'Data', then just save it.
//...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
PrintWriter writer = new PrintWriter(myJSON);
for (Manager m : managerList) {
for (Object o : m.GetCollection()) {
Data d = (Data) o;
// expected result, a json with "foo": bar
writer.println(gson.toJson(d)); // this doesn't work, throws a stack overflow
}
writer.close();
}
Unfortunately, this approach is not working. It, for some weird reason, throws a Stack Overflow. What might be a better approach for this?
Edit: I won't handle the interface directly since foo, or any other values will change during the course of the program.
java gson config
java gson config
edited Nov 26 '18 at 0:13
Frontear
asked Nov 26 '18 at 0:07
FrontearFrontear
502417
502417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've managed to avoid this issue by making use of the @Expose annotation:
public MyData {
@Expose public String foo;
}
Then, when creating the Gson using GsonBuilder, just use excludeFieldWithoutExposeAnnotation()
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473268%2fhow-to-parse-a-specified-class-which-extends-a-base-class-using-gson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've managed to avoid this issue by making use of the @Expose annotation:
public MyData {
@Expose public String foo;
}
Then, when creating the Gson using GsonBuilder, just use excludeFieldWithoutExposeAnnotation()
.
add a comment |
I've managed to avoid this issue by making use of the @Expose annotation:
public MyData {
@Expose public String foo;
}
Then, when creating the Gson using GsonBuilder, just use excludeFieldWithoutExposeAnnotation()
.
add a comment |
I've managed to avoid this issue by making use of the @Expose annotation:
public MyData {
@Expose public String foo;
}
Then, when creating the Gson using GsonBuilder, just use excludeFieldWithoutExposeAnnotation()
.
I've managed to avoid this issue by making use of the @Expose annotation:
public MyData {
@Expose public String foo;
}
Then, when creating the Gson using GsonBuilder, just use excludeFieldWithoutExposeAnnotation()
.
answered Nov 26 '18 at 1:02
FrontearFrontear
502417
502417
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473268%2fhow-to-parse-a-specified-class-which-extends-a-base-class-using-gson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown