Skip to content

Instantly share code, notes, and snippets.

@Guseyn
Last active March 23, 2020 10:24
Show Gist options
  • Save Guseyn/ba8190fad8a85345deb96232ad4957c9 to your computer and use it in GitHub Desktop.
Save Guseyn/ba8190fad8a85345deb96232ad4957c9 to your computer and use it in GitHub Desktop.
Declarative approach via getters
class User {
private String name;
private int age;
// ... constructor
public String name() {
return this.name;
}
public int age() {
return this.age
}
}
class JSONUser {
private User;
// constructor...
public void value() {
return String.format("{ \"name\":\"%s\", \"age\":\"%d\" }", this.user.name(), this.user.age())
}
}
new JSONUser(
new User('John', 26)
).value()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment