Package com.google.gson.stream

Examples of com.google.gson.stream.JsonWriter


    return a;
  }

  @Override
  public void writeNotNull(Annotation a, SerializationContext context) throws Exception {
    JsonWriter writer = context.getWriter().beginObject().name(TYPE_KEY);
    if (a instanceof AnnotationInfo) {
      // Support for UnknownAnnotation
      AnnotationInfo info = (AnnotationInfo) a;
      writer.value(info.getAnnotationTypeName());
      for (Map.Entry<String, Object> entry : info.getAnnotationValues().entrySet()) {
        writer.name(entry.getKey());
        dynamicCodex.write(entry.getValue(), context);
      }
    } else {
      // The usual case
      Class<? extends Annotation> annotationType = a.annotationType();
      writer.value(annotationType.getName());
      for (Method m : annotationType.getDeclaredMethods()) {
        m.setAccessible(true);
        Object value = m.invoke(a);
        writer.name(m.getName());
        typeContext.getCodex(m.getReturnType()).write(value, context);
      }
    }
    writer.endObject();
  }
View Full Code Here


    return oldValue;
  }

  @Override
  public void writeNotNull(Map<K, V> object, SerializationContext context) throws IOException {
    JsonWriter writer = context.getWriter();
    writer.beginObject();
    for (Map.Entry<K, V> entry : object.entrySet()) {
      context.pushPath("[" + entry.getKey() + "]");
      try {
        StealingSerializationContext stealing = new StealingSerializationContext(context);
        keyCodex.write(entry.getKey(), stealing);
        JsonElement keyJson = stealing.getWritten();

        writer.name(keyJson.getAsString());
        valueCodex.write(entry.getValue(), context);
      } finally {
        context.popPath();
      }
    }
    writer.endObject();
  }
View Full Code Here

  }

  @Override
  public <T> boolean visit(FlatPackEntity<T> entity, Codex<T> codex,
      VisitorContext<FlatPackEntity<T>> ctx) {
    JsonWriter json = context.getWriter();
    try {
      json.beginObject();

      // data : { typeName : [ { entity }, { entity } ]
      json.name("data");
      json.beginObject();
      for (Map.Entry<Class<? extends HasUuid>, List<HasUuid>> entry : collate(
          context.getEntities()).entrySet()) {
        json.name(typeContext.describe(entry.getKey()).getTypeName());
        json.beginArray();
        for (HasUuid value : entry.getValue()) {
          if (persistenceMapper.isPersisted(value)) {
            persistent.add(value);
          }
          visitors.visit(this, value);
        }
        json.endArray();
      }
      json.endObject(); // end data

      // value : ['type', 'uuid']
      json.name("value");
      codex.write(entity.getValue(), context);

      // errors : { 'foo.bar.baz' : 'May not be null' }
      Set<ConstraintViolation<?>> violations = entity.getConstraintViolations();
      Map<String, String> errors = entity.getExtraErrors();
      if (!violations.isEmpty() || !errors.isEmpty()) {
        json.name("errors");
        json.beginObject();
        for (ConstraintViolation<?> v : violations) {
          json.name(v.getPropertyPath().toString());
          json.value(v.getMessage());
        }
        for (Map.Entry<String, String> entry : errors.entrySet()) {
          json.name(entry.getKey()).value(entry.getValue());
        }
        json.endObject(); // errors
      }

      // Write metadata for any entities
      if (!persistent.isEmpty()) {
        json.name("metadata");
        json.beginArray();
        for (HasUuid toWrite : persistent) {
          EntityMetadata meta = new EntityMetadata();
          meta.setPersistent(true);
          meta.setUuid(toWrite.getUuid());
          visitors.visit(this, meta);
        }
        json.endArray(); // metadata
      }

      // Write extra top-level data keys, which are only used for simple side-channel data
      for (Map.Entry<String, String> entry : entity.getExtraData().entrySet()) {
        json.name(entry.getKey()).value(entry.getValue());
      }

      // Write extra warnings, some of which may be from the serialization process
      Map<UUID, String> codexWarnings = context.getWarnings();
      Map<String, String> warnings = entity.getExtraWarnings();
      if (!codexWarnings.isEmpty() || !warnings.isEmpty()) {
        json.name("warnings");
        json.beginObject();
        for (Map.Entry<UUID, String> entry : codexWarnings.entrySet()) {
          json.name(entry.getKey().toString()).value(entry.getValue());
        }
        for (Map.Entry<String, String> entry : warnings.entrySet()) {
          json.name(entry.getKey()).value(entry.getValue());
        }
        json.endObject(); // warnings
      }
      json.endObject(); // core payload

    } catch (IOException e) {
      context.fail(e);
    }
View Full Code Here

    return oldValue;
  }

  @Override
  public void writeNotNull(Map<K, V> object, SerializationContext context) throws IOException {
    JsonWriter writer = context.getWriter();
    writer.beginObject();
    for (Map.Entry<K, V> entry : object.entrySet()) {
      String key = entry.getKey().getUuid().toString();
      context.pushPath("." + key);
      try {
        writer.name(key);
        valueCodex.write(entry.getValue(), context);
      } finally {
        context.popPath();
      }
    }
    writer.endObject();
  }
View Full Code Here

  /**
   * Must be paired with a call to {@link #closeContext()}.
   */
  protected SerializationContext serializationContext(FlatPackEntity<?> entity, Writer out) {
    JsonWriter json = new JsonWriter(out);
    json.setLenient(true);
    json.setIndent("  ");
    json.setSerializeNulls(false);
    packScope.enter().withEntity(entity).withJsonWriter(json);
    return serializationContexts.get();
  }
View Full Code Here

   * @param entity the entity to write
   * @param principal an optional Principal for access control
   * @param out the destination output which will not be closed by this method
   */
  public void append(HasUuid entity, Principal principal, Writer out) throws IOException {
    JsonWriter json = jsonWriter(out);

    packScope.enter()
        .withJsonWriter(json)
        .withPrincipal(principal)
        .withTraversalMode(TraversalMode.SPARSE);
View Full Code Here

   *
   * @param entity the entity to write
   * @param out the destination output which will be closed by this method
   */
  public void pack(FlatPackEntity<?> entity, Writer out) throws IOException {
    JsonWriter json = jsonWriter(out);

    packScope.enter().withEntity(entity).withJsonWriter(json);
    try {
      SerializationContext context = contexts.get();
      doPack(entity, context);
View Full Code Here

    }
  }

  private JsonWriter jsonWriter(Writer out) {
    out = ioObserver.observe(out);
    JsonWriter json = new JsonWriter(out);
    json.setSerializeNulls(false);
    if (prettyPrint) {
      json.setIndent("  ");
    }
    return json;
  }
View Full Code Here

    }

    // Write a simple manifest file of all fragments
    try {
      File manifestFile = new File(apidocOutputDir, "manifest");
      JsonWriter writer = new JsonWriter(new OutputStreamWriter(
          buildContext.newFileOutputStream(manifestFile), UTF8));
      writer.beginObject();
      for (Map.Entry<String, String> entry : manifest.entrySet()) {
        writer.name(entry.getKey());
        writer.value(entry.getValue());
      }
      writer.endObject();
      writer.close();
    } catch (Exception e) {
      buildContext.addMessage(apidocDirectory, 0, 0, "Colud not generate manifest",
          BuildContext.SEVERITY_ERROR, e);
    }
  }
View Full Code Here

    }
    return text.toString();
  }

  private void examineClass(ClassDoc clazz) throws IOException {
    JsonWriter writer = getJsonWriter(clazz);

    String classKey = key(clazz);
    // Possibly extract the class's contents
    if (hasAnnotation(clazz, EXAMPLE_TYPE_NAME)) {
      String contents = extractContents(clazz);
      writer.name(classKey + ":contents");
      writer.value(contents);
    }
    String doc = docString(clazz);
    if (!doc.isEmpty()) {
      writer.name(classKey);
      writer.value(doc);
    }

    for (FieldDoc f : clazz.fields(true)) {
      doc = docString(f);
      if (doc.isEmpty()) {
        continue;
      }
      writer.name(key(f));
      writer.value(doc);
    }

    for (MethodDoc m : clazz.methods(true)) {
      String methodKey = key(m);

      // Possibly extract the method's contents
      if (hasAnnotation(m, EXAMPLE_TYPE_NAME)) {
        String contents = extractContents(m);
        writer.name(methodKey + ":contents");
        writer.value(contents);
      }

      doc = docString(m);
      if (doc.isEmpty()) {
        continue;
      }
      writer.name(methodKey);
      writer.value(doc);

      Map<String, Integer> namesToPositions = new HashMap<String, Integer>();
      for (Parameter param : m.parameters()) {
        namesToPositions.put(param.name(), namesToPositions.size());
      }
      for (ParamTag tag : m.paramTags()) {
        Integer position = namesToPositions.get(tag.parameterName());
        // Handle @param tags for non-existant parameters
        if (position == null) {
          continue;
        }
        writer.name(methodKey + "[" + position + "]");
        writer.value(docString(tag));
      }
    }
  }
View Full Code Here

TOP

Related Classes of com.google.gson.stream.JsonWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.