4446

Parsing json into a nested ArrayList in libgdx

Question:

I was using Jackson for all my json serialisation and deserialisation, but I'm trying to get my game working with GWT now so I'm moving over to the libgdx json parsing libraries.

Everything seems ok so far except this

HashMap<String, ArrayList<HighScoreEntry>> high_scores = new HashMap<String, ArrayList<HighScoreEntry>>();

The ArrayList within the hashmap is being created as a array of JsonValue rather than an array of HighScoreEntry.

Can someone explain how I work around this? I know about json.setElementType(); but can't see how to use it in this instance. I'm playing with writing custom serialisation, but again, I can't work out how to extract exactly what I need.

I'm guessing in a custom serialiser I can use

json.readFields(this, jsonData);

to populate everything and then correct the erroneous data afterwards.

HighScoreEntry class (without methods):

public class HighScoreEntry implements Comparable<HighScoreEntry>, java.io.Serializable { public long id; public int score; public String language = "en"; public String data; public String name; public boolean current; }

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/XWvwJ.png" data-original="https://i.stack.imgur.com/XWvwJ.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Pointers would be appreciated.

Answer1:

I've worked something out, but I feel like there must be a better way. If anyone else has any ideas then please post them.

Adding a custom reader I can correct the corrupted high scores and convert them into instances of HighScoreEntry objects.

@Override public void read(Json json, JsonValue jsonData) { // Read all the fields json.readFields(this, jsonData); // replace high scores HighScoreHashMapJsonValue screwedUpHashMap = json.readValue(HighScoreHashMapJsonValue.class, jsonData.get("high_scores")); HashMap<String, Array<HighScoreEntry>> newHighScores = new HashMap<String, Array<HighScoreEntry>>(); for (Map.Entry<String, Array<JsonValue>> entry : screwedUpHashMap.entrySet()) { String key = entry.getKey(); Array<JsonValue> jsonValueHighScores = entry.getValue(); Array<HighScoreEntry> highScoreArray = new Array<HighScoreEntry>(); newHighScores.put(key, highScoreArray); for (JsonValue highScore : jsonValueHighScores) { highScoreArray.add(json.readValue(HighScoreEntry.class, highScore)); } } high_scores = newHighScores; } public static class HighScoreHashMapJsonValue extends HashMap<String, Array<JsonValue>> { }

Recommend

  • Google mock global mock object memory leak
  • Get the object value based on input objects in javascript
  • Rotate Large bitmap using matrix
  • Reading from the Pipeline Stream in PowerShell
  • CSV Parser in one routine/function?
  • Navigate user to New Screen on listitem click React native
  • How to change the Assemby and the Object type on the Newtonsoft.Json serialization/deserialization?
  • Java Parser Json as array multidimensional
  • Python list of (node_id, parent_node_id) to JSON
  • Why is this jQuery reference '$(“”)' instead of '$(“”)'?
  • doing database write after the response
  • Name hiding in constructor initialization list
  • How to separate filename from path? basename() versus preg_split() with array_pop()
  • How to extract a number from a string [duplicate]
  • Set the default timezone in symfony
  • How to use the resource module to measure the running time of a function?
  • Double dispatch in Java example
  • Google Places API - Find a company's CID and LRD
  • jQuery ready not fired after rails link_to is clicked
  • Debug.DrawLine not showing in the GameView
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • Display images in Django
  • Converting a WriteableBitmap image ToArray in UWP
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Moving mysql files across servers
  • $wpdb not working in file of WordPress plugin
  • How to use an array of arrays with array_map(…) in PHP?
  • Paperclip, set path outside of rails root folder
  • R - Combining Columns to String Based on Logical Match
  • Get one-time binding to work for ng-if
  • Bug in WPF DataGrid
  • Running a C# exe file
  • Redux, normalised entities and lodash merge
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • how does django model after text[] in postgresql [duplicate]
  • need help with bizarre java.net.HttpURLConnection behavior
  • How to load view controller without button in storyboard?