Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.theoryinpractise.halbuilder.json;
  
 
 import java.io.Reader;
 import java.util.List;
 import java.util.Map;
 
 import static com.theoryinpractise.halbuilder.impl.api.Support.CURIES;
 import static com.theoryinpractise.halbuilder.impl.api.Support.EMBEDDED;
 import static com.theoryinpractise.halbuilder.impl.api.Support.HREF;
 import static com.theoryinpractise.halbuilder.impl.api.Support.HREFLANG;
 import static com.theoryinpractise.halbuilder.impl.api.Support.LINKS;
 import static com.theoryinpractise.halbuilder.impl.api.Support.NAME;
 import static com.theoryinpractise.halbuilder.impl.api.Support.PROFILE;
 import static com.theoryinpractise.halbuilder.impl.api.Support.TITLE;
 
 public class JsonRepresentationReader implements RepresentationReader {
   private final ObjectMapper mapper;
 
 
   public JsonRepresentationReader(AbstractRepresentationFactory representationFactory) {
     this. = representationFactory;
     this. = new ObjectMapper();
   }
 
   public ContentRepresentation read(Reader reader) {
     try {
       String source = CharStreams.toString(reader);
 
       JsonNode rootNode = .readValue(new StringReader(source), JsonNode.class);
 
       return readResource(rootNode);
     } catch (Exception e) {
       throw new RepresentationException(e);
     }
 
   }
 
   private ContentRepresentation readResource(JsonNode rootNodethrows IOException {
 
 
     readNamespaces(resourcerootNode);
     readLinks(resourcerootNode);
     readProperties(resourcerootNode);
     readResources(resourcerootNode);
     return resource;
   }
 
   private void readNamespaces(MutableRepresentation resourceJsonNode rootNode) {
     if (rootNode.has()) {
       JsonNode linksNode = rootNode.get();
       if (linksNode.has()) {
         JsonNode curieNode = linksNode.get();
 
         if (curieNode.isArray()) {
           Iterator<JsonNodevalues = curieNode.elements();
           while (values.hasNext()) {
             JsonNode valueNode = values.next();
             resource.withNamespace(valueNode.get().asText(), valueNode.get().asText());
           }
         } else {
           resource.withNamespace(curieNode.get().asText(), curieNode.get().asText());
         }
       }
     }
   }
 
   private void readLinks(MutableRepresentation resourceJsonNode rootNode) {
     if (rootNode.has()) {
       Iterator<Map.Entry<StringJsonNode>> fields = rootNode.get().fields();
       while (fields.hasNext()) {
         Map.Entry<StringJsonNodekeyNode = fields.next();
         if (!.equals((keyNode.getKey()))) {
           if (keyNode.getValue().isArray()) {
             Iterator<JsonNodevalues = keyNode.getValue().elements();
             while (values.hasNext()) {
               JsonNode valueNode = values.next();
               withJsonLink(resourcekeyNodevalueNode);
             }
           } else {
             withJsonLink(resourcekeyNodekeyNode.getValue());
           }
        }
      }
    }
  }
  private void withJsonLink(MutableRepresentation resourceMap.Entry<StringJsonNodekeyNodeJsonNode valueNode) {
    String rel = keyNode.getKey();
    String href = valueNode.get().asText();
    String name = optionalNodeValueAsText(valueNode);
    String title = optionalNodeValueAsText(valueNode);
    String hreflang = optionalNodeValueAsText(valueNode);
    String profile = optionalNodeValueAsText(valueNode);
    resource.withLink(relhrefnametitlehreflangprofile);
  }
    JsonNode value = node.get(key);
    return value != null ? value.asText() : null;
  }
  private void readProperties(MutableRepresentation resourceJsonNode rootNodethrows IOException {
    Iterator<StringfieldNames = rootNode.fieldNames();
    while (fieldNames.hasNext()) {
      String fieldName = fieldNames.next();
      if (!..contains(fieldName)) {
        JsonNode field = rootNode.get(fieldName);
        if(field.isArray()) {
            List<ObjectarrayValues = new ArrayList<Object>(field.size());
            for(JsonNode arrayValue : field) {
                arrayValues.add(readResource(arrayValue));
            }
            resource.withProperty(fieldNamearrayValues);
        } else {
            resource.withProperty(fieldNamefield.isNull()
              ? null
              : ( !field.isContainerNode() ? field.asText() : ImmutableMap.copyOf(.readValue(field.toString(), Map.class))));
        }
      }
    }
  }
  private void readResources(MutableRepresentation resourceJsonNode rootNodethrows IOException {
    if (rootNode.has()) {
      Iterator<Map.Entry<StringJsonNode>> fields = rootNode.get().fields();
      while (fields.hasNext()) {
        Map.Entry<StringJsonNodekeyNode = fields.next();
        if (keyNode.getValue().isArray()) {
          Iterator<JsonNodevalues = keyNode.getValue().elements();
          while (values.hasNext()) {
            JsonNode valueNode = values.next();
            resource.withRepresentation(keyNode.getKey(), readResource(valueNode));
          }
        } else {
          resource.withRepresentation(keyNode.getKey(), readResource(keyNode.getValue()));
        }
      }
    }
  }
New to GrepCode? Check out our FAQ X