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

  • Home
  • SEARCH
  • 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 6042533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:48:04+00:00 2026-05-23T06:48:04+00:00

Here is my problem. I have a bunch of Configuration classes that get serialized

  • 0

Here is my problem.

I have a bunch of Configuration classes that get serialized by Gson. They’re all located in one directory, and the serialization/deserialization processes are similar enough that I felt I should move the code up into parent classes.

I eventually came up with this (I feel it is horribly contrived):\

FooConfiguration.java:

package com.bar.foo;

import java.io.File;
import java.io.IOException;

public interface FooConfiguration {
    /**
     * Saves the configuration object to disk
     * @param location the location to save the configuration
     */
    public void save(File location) throws IOException;
}

FooConfigurationAbstract.java:

package com.bar.foo;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.google.gson.Gson;

public abstract class FooConfigurationAbstract implements FooConfiguration {
    File location;
    Gson gson;

    @Override
    public void save(File location) throws IOException {
        FileUtils.writeStringToFile(location, gson.toJson(this), "utf-8");
    }
}

FooConfigurationImpl.java:

package com.bar.foo;

- snip imports -

public class FooConfigurationImpl extends FooConfigurationAbstract {

    /**
     * Whether or not the we should use the new Bar feature
     */
    @Expose
    public Boolean useBar = false;

    - snip more configuration values -
}

FooConfigurationFactory.java:

package com.bar.foo;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FooConfigurationFactory<T extends FooConfiguration> {
    public static Gson gson = new GsonBuilder()
            .setPrettyPrinting()
            .excludeFieldsWithoutExposeAnnotation()
            .create();
    public Class<T> clazz;
    public File basePath;

    public FooConfigurationFactory(File basePath, Class<T> clazz) {
        this.basePath = basePath;
        this.clazz = clazz;
    }

    public T load(String location) throws IOException {
        return this.load(location, FooConfigurationFactory.gson);
    }

    public T load(String location, Gson gson) throws IOException {
        return gson.fromJson(
                FileUtils.readFileToString(
                    new File(this.basePath, location), "utf-8"),
                this.clazz);
    }
}

Example Usage:

this.config = new FooConfigurationFactory<FooConfigurationImpl>(this.configDir, FooConfigurationImpl.class).load("config.json");

I feel like this is the most ugly thing I have seen in my whole life. Is my approach simply wrong, or is there a better way to do it?

  • 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-23T06:48:05+00:00Added an answer on May 23, 2026 at 6:48 am
    • You can simplify the hierarchy by moving save to a separate class. (I don’t think that saving configuration to disk is a responsibility of the configuration itself.)
    • Do you actually need generic configuration factory? You can simplify usage syntax by using generic methods instead of generic class.

    .

    public class FooConfigurationService {
        ...
        public void save(File location, FooConfiguration configuration) { ... }    
        public <T extends FooConfiguration> T load(File location, Class<? extends T> clazz) { ... }
    }
    
    ...
    
    FooConfigurationFactory factory = ...;
    this.config = factory.load(location, FooConfigurationImpl);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of different DTO classes. They are being serialized into an
Here's my problem: I have a virtual method defined in a .h file that
Here is the problem: We have all of our development under subversion, but our
Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
Here's my problem. I have a bunch of figures already written in a LaTeX
Here's the problem. I have a bunch of large text files with paragraphs and
I have two classes. One is a management class, which stores a bunch of
Here's the problem, I have a bunch of directories like S:\HELLO\HI S:\HELLO2\HI\HElloAgain On the
Here is the problem I have: I have a lot (tens of thousands) of
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button

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.