Does GSON access private members of a class directly without invoking its getter methods to fetch the value ?
The rule of Java that is followed is if class B has a private member it cannot be accessed by class A without getter/setter methods.
Now I was working on a project with GSON where in I get a feel getter/setter methods are being bypassed [not used,and private member is directly accessed]
I'm just a student so I could possibly be missing some common working.
Class A:
public class A {
public static void main(String[] args) {
B b = new B();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(b));
System.out.println("--end--");
}
}
Class B:
public class B {
private int a;
public B() {
a = 1;
}
public int getA() {
System.out.println("I am invoked!");
return 10;
}
public void setA(int a) {
this.a = a;
}
}
Note : B.a
is assigned value of 1
, and I coded getA()
to always return 10
If I access B.a
from class A
, EVERYTHING WORKS AS EXPECTED and it wont let me do it , all fine till here
GSON doubt starts here
public class A {
public static void main(String[] args) {
B b = new B();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(b));
System.out.println("--end--");
}
}
Since B.a
is private it is supposed to invoke the getter method that is coded to always return 10 BUT the output I get is
Output:
{
"a": 1
}
--end--
so in other words my getter method for the private method is NEVER INVOKED
In this example you'll see how the Gson library handles the object fields. For object fields to be serialized into JSON string it doesn't need to use any annotations, it can even read private fields.
Yes it uses reflection. Yes reflection is slower on Android. I am using Gson in an Android project and deserializing 10k+ entities from single network calls.
Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it. Gson instances are Thread-safe so you can reuse them freely across multiple threads.
3. Deserialize JSON With Extra Unknown Fields to Object. As you can see, Gson will ignore the unknown fields and simply match the fields that it's able to.
What you're talking about is encapsulation. This is an Object Orientated concept that restricts the access of certain class members.
However, Java has another way of accessing the values in an object. That is, reflection. This allows for a more generic access to a class. For example, in the case of Gson, they need to know about each field in a class, but they can't write a method for every class in existence. Likewise, they can't ask you to write a JSON adapter for it. That defeats the point of the framework.
In comes Reflection. It allows the Gson library to have a single method that converts any object into some JSON. This is important because the Reflections library provides an API for getting at private members of a class without going through the accessors and mutators.
Extra Reading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With