0

I have big chunk of data, that I'd like to make it an object in java (E.g. https://haste.razvancode.com/agiyamuyol.json)

I'm running this code:

        ObjectMapper mapper = new ObjectMapper();
        
        File f = new File("example.json");
        if (!f.exists()) f.createNewFile();

        Board board = mapper.readValue(f, Board.class);

        System.out.println(board.getName());

and I get this error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "isTemplate" (class com.razvancode.discordbot.Utils.Board$Prefs), not marked as ignorable (21 known properties: "calendarFeedEnabled", "voting", "backgroundBottomColor", "cardAging", "backgroundImage", "background", "canBePrivate", "canBeOrg", "comments", "permissionLevel", "selfJoin", "canInvite", "invitations", "backgroundTopColor", "backgroundBrightness", "hideVotes", "cardCovers", "canBeEnterprise", "backgroundTile", "canBePublic", "backgroundImageScaled"])
 at [Source: (File); line: 35, column: 23] (through reference chain: com.razvancode.discordbot.Utils.Board["prefs"]->com.razvancode.discordbot.Utils.Board$Prefs["isTemplate"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.impl.InnerClassProperty.deserializeAndSet(InnerClassProperty.java:90)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2902)
    at com.razvancode.discordbot.Test.<init>(Test.java:28)
    at com.razvancode.discordbot.Test.main(Test.java:34)

Process finished with exit code 1

I'm 100% sure that is from my Board class, but I'm working for hours now and I still can't get it to work.

Boardclass:

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

@NoArgsConstructor
@AllArgsConstructor
public class Board {

    @Getter
    private Object descData, pinned, datePluginDisable, idBoardSource, limits, templateGallery, ixUpdate, idEnterprise, idMemberCreator;
    @Getter
    private String shortUrl, dateLastActivity, shortLink, creationMethod, idOrganization, dateLastView, id, url, name, desc;
    @Getter
    private boolean subscribed, starred, enterpriseOwned, closed;
    @Getter
    private ArrayList<Memberships> memberships;
    @Getter
    private ArrayList<String> idTags, powerUps, premiumFeatures;
    @Getter
    private LabelNames labelNames;
    @Getter
    private Prefs prefs;

    @NoArgsConstructor
    @AllArgsConstructor
    public static class LabelNames {

        @Getter
        private String orange, red, sky, pink, green, blue, lime, yellow, black, purple;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Prefs {

        @Getter
        private String backgroundBrightness, comments, backgroundTopColor, backgroundImage, backgroundBottomColor, voting, permissionLevel, cardAging, invitations, background;
        @Getter
        private boolean canBeEnterprise, hideVotes, canBeOrg, calendarFeedEnabled, backgroundTile, canBePublic, canBePrivate, canInvite, isTemplate, cardCovers, selfJoin;
        @Getter
        private ArrayList<BackgroundImageScaled> backgroundImageScaled;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class BackgroundImageScaled {

        @Getter
        private String url;
        @Getter
        private Long width, height;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Memberships {

        @Getter
        private String idMember, id, memberType;
        @Getter
        private boolean unconfirmed, deactivated;
    }
}

If you have any ideas on how can I fix it, or where I was wrong please tell me.

T. Razvan
  • 21
  • 3

1 Answers1

0

It might be related to this answer. You might need to stop lombok from generating the getter as isTemplate() rather than isIsTemplate(), given that jackson will assume the boolean field in the data is called template.

Hopey One
  • 1,681
  • 5
  • 10
  • It's not related to that post, The problem is on Board#Prefs#boolean values, if I set them to public it works fine, but I don't want them to be public, so if I set them to private and set Boolean instead of boolean it works again, but this gives me getIsTemplate() instead isTemplate(), and I don't think I can specify to lombok that they are boolean values not Boolean... I've tried with @Accessors(fluent = true) but then it works for the boolean values and doesn't for the Strings – T. Razvan Nov 13 '20 at 09:16