Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:49:56+00:00 2026-05-26T08:49:56+00:00

Suppose I have a superclass Entity and some subclasses Creature , Heroes etc. I

  • 0

Suppose I have a superclass Entity and some subclasses Creature,Heroes etc. I have all the data for the subclasses in JSON files which contain arrays that represent each subclass, for example the file json/creatures/a.json represent all the Creatures that are of type A. I’m parsing the files using gson. Here’s what an example file might look like:

[
    {
        "name":         "Pikeman",
        "attack":       4,
        "defence":      5,
        // ...
    },
    {
        "name":         "Halberdier",
        "attack":       6,
        "defence":      5,
    }
]

Now I was thinking that I could make a method in Entity which parses a given JSON file and returns an instance of one of Entity’s subclasses with the data it parsed. If the file only contained one entity, I could do something like

public static Entity parseFromJson(File file, Class<? extends Entity> c) {
    return gson.fromJson(new FileReader(file), c);
}

But now it gets complicated: The files contain arrays of the subclasses. Should I pass Class<? extends Entity[]> and make the return type Entity[] instead? If so, then where and how should I access a single element of that array? Or should I rather have just one creature per file and send the name as a string instead?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:49:56+00:00Added an answer on May 26, 2026 at 8:49 am

    You could try

    public static <T extends Entity> T parseFromJSON(File file, Class<T> clazz) {
        return clazz.cast(gson.FromJson(new FileReader(file), clazz));
    }
    
    public static <T extends Entity> T[] parseFromJSONArray(File file, Class<T[]> clazz) {
        return clazz.cast(gson.FromJson(new FileReader(file), clazz));
    }
    

    Which you could use something like (given that gson accepts it)

    Foo foo = parseFromJSON(file, Foo.class);
    Foo[] foos = parseFromJSONArray(file, Foo[].class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a common superclass (suppose it's Employee ), which has (amongst others) subclasses
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a superclass Item and a subclass MovingItem. If I create an
Suppose I have MyEdge and MyEdgeModified , such that MyEdge is the superclass and
Suppose I have a class MyClass to which I want to add certain 'observer'
If I have a class hierarchy in which subclasses require use of more specific
Suppose I have a FruitDetector class which takes in a string and returns the
Suppose you have legacy java code which can not be compiled by an up-to-date
Suppose I have a number of related classes that all have a method like
suppose I have {data: {243232: {id: testid,name: test } }} so how to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.