0

I have this code:

public enum Make{
  FORD, BMW, AUDI
}

@Entity
public class User{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  
  private String email;
 
  ...
  @OneToMany(...)
  private Set<Cars> favoriteMakes = new HashSet<>();

}

I would like to be able to update a user by sending json with a list of favorite makes. F.e.: New user:

{
   "id" : 1,
   "email" : "email@mail.com",
    ...
   "favoriteMakes" : []
}

I want to achieve :

{
   "id" : 1,
   "email" : "email@mail.com",
    ...
   "favoriteMakes" : ["FORD", "AUDI"]
}

The things I've tried:

"favoriteMakes" : ["FORD", "AUDI"],
"favoriteMakes" : ["0", "2"],
"favoriteMakes" : [0, 2],
Keetch
  • 75
  • 1
  • 9

1 Answers1

0

I created a short example just to show how to serialize and deserialize Json using newtonsoft

    public enum Make
    {
        FORD, BMW, AUDI
    }
    public partial class User
    {
        [JsonProperty("id")]
        public long Id { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("favoriteMakes")]
        public Make[] FavoriteMakes { get; set; }
    }


    class Program
    {
        public static void Main(String[] args)
        {

            var myUser = new User();
            myUser.FavoriteMakes = new Make[1];
            myUser.FavoriteMakes[0] = Make.AUDI;
            var sz = JsonConvert.SerializeObject(myUser);
            User dsz = JsonConvert.DeserializeObject<User>(sz);
        }

        

    }
InUser
  • 1,138
  • 15
  • 22
  • Thank you! Forgot to add, that this is Java. The thing is that I want to do it with a put request, as I am creating a REST API – Keetch Jun 24 '20 at 08:11
  • how about this? -> https://stackoverflow.com/questions/30871013/java-convert-object-consisting-enum-to-json-object – InUser Jun 24 '20 at 08:26