private static void writeView(View view, JsonObject json) {
    writeString("viewType", view.getClass().getCanonicalName(), json);

    writeString("viewName", view.getViewName(), json);

    writeString("generator", view.getViewGenerator(), json);

    if (view.getScore() != 0) writeDouble("score", view.getScore(), json);

    List<Constituent> constituents = view.getConstituents();

    if (constituents.size() > 0) {
      JsonArray cJson = new JsonArray();
      for (int i = 0; i < view.getNumberOfConstituents(); i++) {

        Constituent constituent = constituents.get(i);
        JsonObject c = new JsonObject();
        writeConstituent(constituent, c);

        cJson.add(c);
      }

      json.add("constituents", cJson);
    }

    List<Relation> relations = view.getRelations();

    if (relations.size() > 0) {

      JsonArray rJson = new JsonArray();

      for (Relation r : relations) {
        Constituent src = r.getSource();
        Constituent tgt = r.getTarget();

        int srcId = constituents.indexOf(src);
        int tgtId = constituents.indexOf(tgt);

        JsonObject rJ = new JsonObject();

        writeString("relationName", r.getRelationName(), rJ);

        if (r.getScore() != 0) writeDouble("score", r.getScore(), rJ);
        writeInt("srcConstituent", srcId, rJ);
        writeInt("targetConstituent", tgtId, rJ);
        writeAttributes(r, rJ);

        Map<String, Double> labelsToScores = r.getLabelsToScores();

        if (null != labelsToScores) writeLabelsToScores(labelsToScores, rJ);

        rJson.add(rJ);
      }

      json.add("relations", rJson);
    }
  }
 /**
  * Add an array of objects reporting View's Constituents' surface form and character offsets. May
  * make deserialization to TextAnnotation problematic, as the relevant methods deduce token
  * character offsets directly from list of token strings and raw text.
  *
  * @param fieldName name to give to this field
  * @param view view whose character offsets will be serialized
  * @param json Json object to which resulting array will be added
  */
 private static void writeTokenOffsets(String fieldName, View view, JsonObject json) {
   JsonArray offsetArray = new JsonArray();
   for (Constituent c : view.getConstituents()) {
     JsonObject cJ = new JsonObject();
     writeString(FORM, c.getSurfaceForm(), cJ);
     writeInt(STARTCHAROFFSET, c.getStartCharOffset(), cJ);
     writeInt(ENDCHAROFFSET, c.getEndCharOffset(), cJ);
     offsetArray.add(cJ);
   }
   json.add(fieldName, offsetArray);
 }