eclipse-ee4j / eclipse-ee4j/yasson

toJson with Type is ignored

Open
#385 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
218
Forks
109
Avg merge
1d 5h
Merged PRs (30d)
9

Description

**Describe the bug**
The JSON-B API specifies a `Jsonb#toJson(Object, Type)` method, but the purpose of this `Type` parameter is not clear to me. I was trying to reverse-engineer the purpose of this parameter by trying some examples with Yasson, but none of the examples show the effect of the `Type` parameter. This may indicate that there's a bug in the implementation.

**To Reproduce**
Below are several example usages of Yasson. In each example, the generated JSON includes all properties of the subclass, whereas we expect only the properties of the type that we specify to show up.

With an interface `Animal`:
```
public class App {
public static void main(String[] args) {
Jsonb jsonb = JsonbBuilder.create();
Cat cat = new Cat("Emma", 2, "Sphynx");
String result = jsonb.toJson(cat, Animal.class);
System.out.println(result); // Prints: {"age":2,"breed":"Sphynx","name":"Emma"}, but I expected to see only the "name" property.
}
public static interface Animal {
public String getName();
}
public static class Cat implements Animal {
public String name;
public int age;
public String breed;
public Cat(String name, int age, String breed) {
this.name = name;
this.age = age;
this.breed = breed;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getBreed() {
return breed;
}
}
}
```

With an abstract class `Animal`:
```
public class App {
public static void main(String[] args) {
Jsonb jsonb = JsonbBuilder.create();
Cat cat = new Cat("Emma", 2, "Sphynx");
String result = jsonb.toJson(cat, Animal.class);
System.out.println(result); // {"age":2,"breed":"Sphynx","name":"Emma"}, but I expect to see only the "name" property
}
public static abstract class Animal {
public abstract String getName();
}
public static class Cat extends Animal {
public String name;
public int age;
public String breed;
public Cat(String name, int age, String breed) {
this.name = name;
this.age = age;
this.breed = breed;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getBreed() {
return breed;
}
}
}
```

With a concrete class:
```
public class App {
public static void main(String[] args) {
Jsonb jsonb = JsonbBuilder.create();
Cat cat = new Cat();
cat.name = "Emma";
cat.age = 2;
cat.breed = "Sphynx";
String result = jsonb.toJson(cat, Animal.class); // {"name":"Emma","age":2,"breed":"Sphynx"}, but I expect to see only the "name" property
System.out.println(result);
}
public static class Animal {
public String name;
}
public static class Cat extends Animal {
public int age;
public String breed;
}
}
```

With a generic collection:
```
public class App {
public static void main(String[] args) {
List animals = new ArrayList<>();
animals.add(new Dog("Falco"));
animals.add(new Cat("Harris", true));
Type type = new ArrayList() {}.getClass().getGenericSuperclass();
Jsonb jsonb = JsonbBuilder.create();
String json = jsonb.toJson(animals, type);
System.out.println(json); // [{"name":"Falco"},{"name":"Harris","b":true}]
String json2 = jsonb.toJson(animals);
System.out.println(json2); // [{"name":"Falco"},{"name":"Harris","b":true}]
}

public static class Animal {
public String name;

public Animal(String name) {
this.name = name;
}
}

public static class Cat extends Animal {
public boolean b;

public Cat(String name, boolean b) {
super(name);
this.b = b;
}
}

public static class Dog extends Animal {
public Dog(String name) {
super(name);
}
}
}
```

**System information:**
- OS: macOS
- Java Version: 8
- Yasson Version: 1.0.6

Contributor guide

Open the contributing guide

Research direction

Start by running the Java examples in the issue against Yasson 1.0.6 and trace the Jsonb#toJson(Object, Type) entry point. Compare serialization with and without the supplied Type, including the interface, concrete class, and generic collection cases; done means the declared Type affects the emitted properties as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.