No, I'm not using YAML, I'm using JSON.
|
Hi, I need help with this as I cannot find out how on earth to do it. Here is a example of what I want to do:
getParameters() is a extension of a JSONObjectCode:... /** * Gets the parameter. * @param parameter The parameter. * @return The paramter's value. */ @SuppressWarnings("unchecked") public <E extends Object> E getParameter(String parameter) { E returnz = null; try { returnz = (E) packet.getParameters().get(parameter); } catch (JSONException e) { e.printStackTrace(); } return returnz; } ...
get returns a Object.
I want to allow its use like so
This would obviously throw a cannot cast integer to string bla bla, how would I go about this?Code:... int id = getParameter("myint"); ...
- I do not want to do Integer.parseInt
Thanks
No, I'm not using YAML, I'm using JSON.
i misread your post
That would require rewriting my whole frontend, which I don't want to do, I would much rather use json.
Reason that I want to be able to print json back to the browser, and that can be read by some javascript.
https://code.google.com/p/google-gson/ may be your solution then (GSON.toJson and GSON.fromJson), although i don't see why your code wouldn't work
taking my advice on using a markup for protocol definitions eh?
if i understand your question correctly, then packet.getParameters() is an instance of JSONObject... why wouldn't parameters.getInt("myint") work?
in the case that you want to maintain some constant interface for all config, you could just do...
to demonstrate:Code:int id = this.<Integer>getParameter("myid");
Code:import java.util.Map; import java.util.HashMap; class Mock { private Map parameters = new HashMap(); private <E> E getParameter(String key) { return (E) parameters.get(key); } private void test() { parameters.put("myint", 10); parameters.put("lol", "go fuck yourself"); int id = this.<Integer>getParameter("myint"); assert id == 10; String txt = getParameter("lol"); assert txt == "go fuck yourself"; } public static void main(String[] argv) { Mock mock = new Mock(); mock.test(); } }
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |