title: Libgdx JSON custom serialization of Array<Vector2>
body: <p>I'm trying to write a custom JSON serializer that can read in an array of floats as the coordinates for Vector2 objects. Suppose I have an array in my class called:</p>
  
  <pre><code>Array&lt;Vector2&gt; splinePoints;
  </code></pre>
  
  <p>I would like to be able to read and write a JSON file formatted like:</p>
  
  <pre><code>splinePoints: [0,0, 1,1, 2,2]
  </code></pre>
  
  <p>Each pair of floats is read as the x and y coordinates of a Vector2 object. This format allows me to copy and paste spline points generated by Inkscape with minimal editing. Also, this is more compact than the form which would work with the default serializer:</p>
  
  <pre><code>splinePoints: [{x: 0, y: 0}, {x: 1, y: 1}, {x: 2, y: 2}]
  </code></pre>
  
  <p>So far, I've tried this code (which is probably very wrong):</p>
  
  <pre><code>public class PointsHolder {
      public Array&lt;Vector2&gt; splinePoints = new Array&lt;Vector2&gt;();
  }
  
  Json json = new Json();
  json.setSerializer(PointsHolder.class, new Json.Serializer&lt;PointsHolder&gt;() {
      public void write(Json json, PointsHolder pointsHolder, Class knownType) {
          json.writeArrayStart();
          for(Vector2 vector2: pointsHolder.splinePoints) {
              json.writeValue(vector2.x);
              json.writeValue(vector2.y);
          }
          json.writeArrayEnd();
      }
  
      public PointsHolder read(Json json, JsonValue jsonData, Class type) {
          PointsHolder pointsHolder = new PointsHolder();
          for (JsonValue child = jsonData.child; child != null; child = child.next) {
              Vector2 vector2 = new Vector2();
              vector2.x = jsonData.child.asFloat();
              vector2.y = jsonData.child.next.asFloat();
              pointsHolder.splinePoints.add(vector2);
          }
  
          return pointsHolder;
      }
  });
  </code></pre>
  
  <p>When run, I get an exception saying it's unable to convert value to required type. Even after reading the tutorials and going through the source code, I'm having trouble understanding how the JSON serializer even works. What does JsonValue jsonData refer to when reading an array? What is the third argument "Class" used for in the read and write methods? Do I need to write a serializer for Array.class instead of Vector2.class, although I would still like to use the default serializer for arrays that aren't of type Vector2?</p>