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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:58:38+00:00 2026-05-15T18:58:38+00:00

I have the following C# helper class which retrieves a Json String from a

  • 0

I have the following C# helper class which retrieves a Json String from a remote server and casts it to the type I need.

public class JsonHelper
{
    private JavaScriptSerializer serializer = new JavaScriptSerializer();

    public T GetValue<T>(string methodName, object param) where T : IConvertible
    {
        string result = GetJsonString(methodName, param);
        return (T)Convert.ChangeType(obj, typeof(T));
    }

    public T GetObject<T>(string methodName, object param) where T : new()
    {
        var result = GetValue<string>(methodName, param);
        return serializer.Deserialize<T>(result);
    }
}

–

// Usage:
bool value1 = GetValue<bool>(methodName, param);
int value2 = GetValue<int>(methodName, param);
double value3 = GetValue<double>(methodName, param);
User user = GetObject<User>(methodName, param);
List<User> users = GetObject<List<User>>(methodName, param);

Now I want to have the same functionality in a java programm. There are two points where I get stuck at the moment:

  • Is this method call correct?

     public <T> T SendJsonPrimitiveRequest(String methodName, Object param)
        { 
            return null;
        }
    

And how do I restrict this generic method to only accept type parameters that can be casted from string.

  • GetValue should be used for retrieving int, double, bool and string values which works great in C# by using Convert.ChangeType(…);
    Is this possible for Java or do I have to write methods like GetBool(), GetInt(), …?
  • 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-15T18:58:39+00:00Added an answer on May 15, 2026 at 6:58 pm

    Unfortunately, Java doesn’t have a built-in interface to describe conversion from a string. The closest is that some objects take a String in the constructor, or have a valueOf(String) static method. As there is no interface to declare this feature, it’s not going to be possible to use generics to restrict the type to objects that can be constructed from a string.

    You can use generics with boxed types to provide a single GetValue method, but it requires also that you pass in the corresponding class of the type you want to retrieve, e.g.

      T <T> getValue(String methodName, Object param, Class<T> type)
    

    Not very pretty, and coupled with the lack of a nice way to generically construct objects from strings, it is probably simplest (and more efficient with no boxing required) to create separate getInt(), getBool() methods.

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

Sidebar

Related Questions

In a makefile, I have the following line: helper.cpp: dtds.h Which ensures that helper.cpp
See the following class public class Parent { private String name; private int age;
I have following string String str = replace :) :) with some other string;
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have an ascx page which contains the following line: <%= Html.TextBox(DayOfWeek, Model.JourneyBooking.StartDate.DayOfWeek.ToString(), new
I have the following code require 'test_helper' class ApplicationControllerTest < ActionController::TestCase test should display
Currently I have the following in my helper: def created_today k if k.created_at.to_date ==

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.