forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/eugenp/tutorials
- Loading branch information
Showing
13 changed files
with
394 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
|
||
public class CustomListDeserializer extends JsonDeserializer<List<ItemWithSerializer>> { | ||
|
||
@Override | ||
public List<ItemWithSerializer> deserialize(final JsonParser jsonparser, final DeserializationContext context) throws IOException, JsonProcessingException { | ||
return new ArrayList<ItemWithSerializer>(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
jackson/src/test/java/org/baeldung/jackson/bidirection/CustomListSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
public class CustomListSerializer extends JsonSerializer<List<ItemWithSerializer>> { | ||
|
||
@Override | ||
public void serialize(final List<ItemWithSerializer> items, final JsonGenerator generator, final SerializerProvider provider) throws IOException, JsonProcessingException { | ||
final List<Integer> ids = new ArrayList<Integer>(); | ||
for (final ItemWithSerializer item : items) { | ||
ids.add(item.id); | ||
} | ||
generator.writeObject(ids); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
jackson/src/test/java/org/baeldung/jackson/bidirection/Item.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
|
||
public class Item { | ||
public int id; | ||
public String itemName; | ||
public User owner; | ||
|
||
public Item() { | ||
super(); | ||
} | ||
|
||
public Item(final int id, final String itemName, final User owner) { | ||
this.id = id; | ||
this.itemName = itemName; | ||
this.owner = owner; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIdentityInfo; | ||
import com.fasterxml.jackson.annotation.ObjectIdGenerators; | ||
|
||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") | ||
public class ItemWithIdentity { | ||
public int id; | ||
public String itemName; | ||
public UserWithIdentity owner; | ||
|
||
public ItemWithIdentity() { | ||
super(); | ||
} | ||
|
||
public ItemWithIdentity(final int id, final String itemName, final UserWithIdentity owner) { | ||
this.id = id; | ||
this.itemName = itemName; | ||
this.owner = owner; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithIgnore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
|
||
public class ItemWithIgnore { | ||
public int id; | ||
public String itemName; | ||
public UserWithIgnore owner; | ||
|
||
public ItemWithIgnore() { | ||
super(); | ||
} | ||
|
||
public ItemWithIgnore(final int id, final String itemName, final UserWithIgnore owner) { | ||
this.id = id; | ||
this.itemName = itemName; | ||
this.owner = owner; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import com.fasterxml.jackson.annotation.JsonManagedReference; | ||
|
||
public class ItemWithRef { | ||
public int id; | ||
public String itemName; | ||
|
||
@JsonManagedReference | ||
public UserWithRef owner; | ||
|
||
public ItemWithRef() { | ||
super(); | ||
} | ||
|
||
public ItemWithRef(final int id, final String itemName, final UserWithRef owner) { | ||
this.id = id; | ||
this.itemName = itemName; | ||
this.owner = owner; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
jackson/src/test/java/org/baeldung/jackson/bidirection/ItemWithSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
|
||
public class ItemWithSerializer { | ||
public int id; | ||
public String itemName; | ||
public UserWithSerializer owner; | ||
|
||
public ItemWithSerializer() { | ||
super(); | ||
} | ||
|
||
public ItemWithSerializer(final int id, final String itemName, final UserWithSerializer owner) { | ||
this.id = id; | ||
this.itemName = itemName; | ||
this.owner = owner; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
jackson/src/test/java/org/baeldung/jackson/bidirection/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class User { | ||
public int id; | ||
public String name; | ||
public List<Item> userItems; | ||
|
||
public User() { | ||
super(); | ||
} | ||
|
||
public User(final int id, final String name) { | ||
this.id = id; | ||
this.name = name; | ||
userItems = new ArrayList<Item>(); | ||
} | ||
|
||
public void addItem(final Item item) { | ||
userItems.add(item); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIdentity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIdentityInfo; | ||
import com.fasterxml.jackson.annotation.ObjectIdGenerators; | ||
|
||
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") | ||
public class UserWithIdentity { | ||
public int id; | ||
public String name; | ||
public List<ItemWithIdentity> userItems; | ||
|
||
public UserWithIdentity() { | ||
super(); | ||
} | ||
|
||
public UserWithIdentity(final int id, final String name) { | ||
this.id = id; | ||
this.name = name; | ||
userItems = new ArrayList<ItemWithIdentity>(); | ||
} | ||
|
||
public void addItem(final ItemWithIdentity item) { | ||
userItems.add(item); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithIgnore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
public class UserWithIgnore { | ||
public int id; | ||
public String name; | ||
|
||
@JsonIgnore | ||
public List<ItemWithIgnore> userItems; | ||
|
||
public UserWithIgnore() { | ||
super(); | ||
} | ||
|
||
public UserWithIgnore(final int id, final String name) { | ||
this.id = id; | ||
this.name = name; | ||
userItems = new ArrayList<ItemWithIgnore>(); | ||
} | ||
|
||
public void addItem(final ItemWithIgnore item) { | ||
userItems.add(item); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithRef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonBackReference; | ||
|
||
public class UserWithRef { | ||
public int id; | ||
public String name; | ||
|
||
@JsonBackReference | ||
public List<ItemWithRef> userItems; | ||
|
||
public UserWithRef() { | ||
super(); | ||
} | ||
|
||
public UserWithRef(final int id, final String name) { | ||
this.id = id; | ||
this.name = name; | ||
userItems = new ArrayList<ItemWithRef>(); | ||
} | ||
|
||
public void addItem(final ItemWithRef item) { | ||
userItems.add(item); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
jackson/src/test/java/org/baeldung/jackson/bidirection/UserWithSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.baeldung.jackson.bidirection; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
public class UserWithSerializer { | ||
public int id; | ||
public String name; | ||
|
||
@JsonSerialize(using = CustomListSerializer.class) | ||
@JsonDeserialize(using = CustomListDeserializer.class) | ||
public List<ItemWithSerializer> userItems; | ||
|
||
public UserWithSerializer() { | ||
super(); | ||
} | ||
|
||
public UserWithSerializer(final int id, final String name) { | ||
this.id = id; | ||
this.name = name; | ||
userItems = new ArrayList<ItemWithSerializer>(); | ||
} | ||
|
||
public void addItem(final ItemWithSerializer item) { | ||
userItems.add(item); | ||
} | ||
} |
Oops, something went wrong.