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 6148449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:17:01+00:00 2026-05-23T19:17:01+00:00

I was writing a quick method that is using Google Gson to translate an

  • 0

I was writing a quick method that is using Google Gson to translate an Object to JSON. It’s simple, but I wanted to centralize my JSON building process in case I want to make further changes:

public static String buildJSONObject(Object obj) {
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    return gson.toJson(obj);
}

And now I have two questions regarding this (which works),

-Is using Object okay as a method argument, or is there a better way using Generics?

-Is there performance overhead if constantly casting Objects to whatever kind of object is passed to the method?

  • 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-23T19:17:01+00:00Added an answer on May 23, 2026 at 7:17 pm

    Is using Object okay as a method argument, or is there a better way using Generics?

    Generics offer type safety and show intent better, leading to cleaner code. That being said, they really only offer these benefits when you know what kinds of objects you want to pass to the method and you have established some kind of object heirarchy which you extend all your JSON’able objects from. If you really want to be able to turn any object under the sun into JSON, Object obj is the way to go.

    Is there performance overhead if constantly casting Objects to whatever kind of object is passed to the method?

    There are no performance problems with casting. What you should be worried about is type safety and RuntimeExceptions from invalid casts. Be careful of doing casts if you are certain of the types of objects going into your method. You can always do a safe cast with a instanceof check first as well which will make this less of a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ended up writing a quick little script for this in Python, but I
I was just writing some quick code and noticed this complier error Using the
I am writing a quick and dirty application that reads all the files from
Is this a simple process? I'm only writing a quick hacky UI for an
I'm currently writing a quick custom encoding method where I take a stamp a
Quick background: writing a module. One of my objects has methods that may or
I was just writing a quick bit of code, and I wanted to use
I'm writing a quick simple table view app. I've declared the view I'm creating
So, here's a funny little programming challenge. I was writing a quick method to
I'm writing a quick one-off web application for a friend using Rails 3.0.9. It's

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.