Example usage for com.fasterxml.jackson.databind.node TextNode TextNode

List of usage examples for com.fasterxml.jackson.databind.node TextNode TextNode

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.node TextNode TextNode.

Prototype

public TextNode(String paramString) 

Source Link

Usage

From source file:com.mapr.synth.samplers.SsnSampler.java

@Override
public JsonNode sample() {
    while (true) {
        int i = rand.nextInt(codes.size());
        List<String> fields = values.get(codes.get(i));

        if (keepTypes.contains(fields.get(names.indexOf("type")))) {
            if (verbose) {
                ObjectNode rx = new ObjectNode(nodeFactory);
                Iterator<String> nx = names.iterator();
                for (String field : fields) {
                    Preconditions.checkState(nx.hasNext());
                    String fieldName = nx.next();
                    if (keepFields.contains(fieldName)) {
                        rx.set(fieldName, new TextNode(field));
                    }// w  w  w.ja va  2 s .com
                }
                Preconditions.checkState(!nx.hasNext());
                if (keepFields.contains("ssn")) {
                    rx.set("ssn", new TextNode(String.format("%s-%02d-%04d", codes.get(i), rand.nextInt(99) + 1,
                            rand.nextInt(9999) + 1)));
                }
                return rx;
            } else {
                return new TextNode(String.format("%s-%02d-%04d", codes.get(i), rand.nextInt(99) + 1,
                        rand.nextInt(9999) + 1));
            }
        }
    }
}

From source file:managers.nodes.SlotManager.java

protected Promise<JsonNode> toJSON(final JsonNode properties) {
    Slot slot = new Slot(properties.get("uuid").asText());
    Promise<List<JsonNode>> partsAndRefs = Has.relationships.endNodes(slot);
    Promise<JsonNode> json = partsAndRefs.map(new Function<List<JsonNode>, JsonNode>() {
        public JsonNode apply(List<JsonNode> partsAndRefs) {
            List<JsonNode> partNodes = new ArrayList<JsonNode>();
            List<JsonNode> refNodes = new ArrayList<JsonNode>();
            for (JsonNode partOrRef : partsAndRefs) {
                if (partOrRef.has("name")) {
                    String name = partOrRef.get("name").asText();
                    JsonNode ref = new TextNode(name);
                    refNodes.add(ref);/*from w w  w .j a  va2 s .  co m*/
                } else {
                    partNodes.add(partOrRef);
                }
            }
            ArrayNode parts = JsonNodeFactory.instance.arrayNode();
            parts.addAll(partNodes);
            ((ObjectNode) properties).put("parts", parts);
            ArrayNode refs = JsonNodeFactory.instance.arrayNode();
            refs.addAll(refNodes);
            ((ObjectNode) properties).put("refs", refs);
            return properties;
        }
    });
    return json;
}

From source file:com.mapr.synth.samplers.VinSampler.java

@Override
public JsonNode sample() {
    ObjectNode r = new ObjectNode(nodeFactory);

    String manufacturer = randomCode(legalCodes);
    String restraint = randomCode(restraintCodes);

    int year = randomCode(legalYears);
    String yearCode = computeYearCode(year);
    int sequence = sequenceCounter.incrementAndGet();

    String front;/*from www . j av a  2  s  .  c  o  m*/
    String plant;

    String make = makes.get(manufacturer);

    switch (make) {
    case "Ford": {
        String model = randomCode(fordModelCodes);
        String engine = randomCode(fordEngineCodes);
        plant = randomCode(fordPlantCodes);
        front = pad(manufacturer, 3, "AAAAAAAAAAAAAAAAAA") + restraint + pad(model, 3, "0000000000000000")
                + engine;
        if (verbose) {
            r.set("model", new TextNode(fordModels.get(model)));
            r.set("engine", new TextNode(fordEngines.get(engine)));
        }
        break;
    }
    case "BMW":
    case "BMW M": {
        String model = randomCode(bmwModelCodes);
        plant = randomCode(bmwPlantCodes);
        front = pad(manufacturer, 3, "AAAAAAAAAAAAAAAAAA") + restraint + model;
        if (verbose) {
            r.set("model", new TextNode(bmwModels.get(model)));
            r.set("plant", new TextNode(bmwPlants.get(plant)));
        }
        break;
    }
    default: {
        String model = gibberish(4);
        plant = gibberish(1);
        front = pad(manufacturer, 3, "AAAAAAAAAAAAAAAAAA") + restraint + model;
        break;
    }
    }
    String check = "0";

    String rawVin = front + check + yearCode + plant + String.format("%06d", sequence);
    String vin = addCheckDigit(rawVin);

    if (verbose) {
        r.set("VIN", new TextNode(vin));
        r.set("manufacturer", new TextNode(makes.get(manufacturer)));
        r.set("year", new IntNode(year));
    } else {
        return new TextNode(vin);
    }
    return r;
}

From source file:com.aol.one.patch.DefaultPatcherTest.java

@Test
public void testAddForSuccess() throws PatchException {
    List<PatchOperation> operations = new ArrayList<>();

    operations.add(new AddOperation("/doubleField", new DoubleNode(10.1d)));
    operations.add(new AddOperation("/strField", new TextNode("1.1String")));
    operations.add(new AddOperation("/child/doubleField", new DoubleNode(102.1)));

    patcher.patch(testObject, operations);

    Assert.assertEquals(10.1d, testObject.getDoubleField(), 0);
    Assert.assertEquals("1.1String", testObject.getStrField());

    Assert.assertEquals(102.1d, testObject.getChild().getDoubleField(), 0);
    Assert.assertEquals("2string", testObject.getChild().getStrField());
}

From source file:com.mapr.synth.samplers.CommonPointOfCompromise.java

@Override
public JsonNode sample() {
    ArrayNode r = nodeFactory.arrayNode();

    double t = start;
    double averageInterval = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS) / transactionsPerDay.nextDouble();
    Exponential interval = new Exponential(1 / averageInterval, gen);

    Date date = new Date();
    boolean compromised = false;
    while (t < end) {
        ObjectNode transaction = new ObjectNode(nodeFactory);
        t += interval.nextDouble();//from  w  w w .  j av a 2s .  c  o  m
        date.setTime((long) t);
        transaction.set("timestamp", new LongNode((long) (t / 1000)));
        transaction.set("date", new TextNode(df.format(date)));
        Integer merchantId = merchant.sample();
        transaction.set("merchant", new IntNode(merchantId));

        if (merchantId == 0 && t >= compromiseStart && t < compromiseEnd) {
            compromised = true;
            transaction.set("compromise", new IntNode(1));
        } else {
            transaction.set("compromise", new IntNode(0));
        }

        if (t > exploitEnd) {
            compromised = false;
        }

        double pFraud;
        if (t >= exploitStart && compromised) {
            pFraud = compromisedFraudRate;
        } else {
            pFraud = uncompromisedFraudRate;
        }

        transaction.set("fraud", new IntNode((gen.nextDouble() < pFraud) ? 1 : 0));

        r.add(transaction);
    }
    return r;
}

From source file:com.msopentech.odatajclient.testservice.utils.JSONUtilities.java

/**
 * {@inheritDoc }//from w w w .  ja v  a2 s .  c  o  m
 */
@Override
protected InputStream normalizeLinks(final String entitySetName, final String entityKey, final InputStream is,
        final NavigationLinks links) throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final ObjectNode srcNode = (ObjectNode) mapper.readTree(is);

    if (links != null) {
        for (String linkTitle : links.getLinkNames()) {
            // normalize link
            srcNode.remove(linkTitle + JSON_NAVIGATION_BIND_SUFFIX);
            srcNode.set(linkTitle + JSON_NAVIGATION_SUFFIX,
                    new TextNode(String.format("%s(%s)/%s", entitySetName, entityKey, linkTitle)));
        }

        for (String linkTitle : links.getInlineNames()) {
            // normalize link if exist; declare a new one if missing
            srcNode.remove(linkTitle + JSON_NAVIGATION_BIND_SUFFIX);
            srcNode.set(linkTitle + JSON_NAVIGATION_SUFFIX,
                    new TextNode(String.format("%s(%s)/%s", entitySetName, entityKey, linkTitle)));

            // remove inline
            srcNode.remove(linkTitle);

            // remove from links
            links.removeLink(linkTitle);
        }
    }

    srcNode.set(JSON_EDITLINK_NAME,
            new TextNode(Constants.DEFAULT_SERVICE_URL + entitySetName + "(" + entityKey + ")"));

    return IOUtils.toInputStream(srcNode.toString());
}

From source file:org.apache.olingo.fit.utils.JSONUtilities.java

@Override
protected InputStream normalizeLinks(final String entitySetName, final String entityKey, final InputStream is,
        final NavigationLinks links) throws IOException {

    final ObjectNode srcNode = (ObjectNode) mapper.readTree(is);

    if (links != null) {
        for (String linkTitle : links.getLinkNames()) {
            // normalize link
            srcNode.remove(linkTitle + Constants.get(ConstantKey.JSON_NAVIGATION_BIND_SUFFIX));
            srcNode.set(linkTitle + Constants.get(ConstantKey.JSON_NAVIGATION_SUFFIX),
                    new TextNode(String.format("%s(%s)/%s", entitySetName, entityKey, linkTitle)));
        }/*from w  w  w  . j  ava  2 s . c  o m*/

        for (String linkTitle : links.getInlineNames()) {
            // normalize link if exist; declare a new one if missing
            srcNode.remove(linkTitle + Constants.get(ConstantKey.JSON_NAVIGATION_BIND_SUFFIX));
            srcNode.set(linkTitle + Constants.get(ConstantKey.JSON_NAVIGATION_SUFFIX),
                    new TextNode(String.format("%s(%s)/%s", entitySetName, entityKey, linkTitle)));

            // remove inline
            srcNode.remove(linkTitle);

            // remove from links
            links.removeLink(linkTitle);
        }
    }

    srcNode.set(Constants.get(ConstantKey.JSON_EDITLINK_NAME), new TextNode(
            Constants.get(ConstantKey.DEFAULT_SERVICE_URL) + entitySetName + "(" + entityKey + ")"));

    return IOUtils.toInputStream(srcNode.toString(), Constants.ENCODING);
}

From source file:io.confluent.kafkarest.unit.AvroConverterTest.java

@Test
public void testPrimitiveTypesToAvro() {
    Object result = AvroConverter.toAvro(null, createPrimitiveSchema("null"));
    assertTrue(result == null);//from   w  w w .  ja  va 2  s.c  om

    result = AvroConverter.toAvro(TestUtils.jsonTree("true"), createPrimitiveSchema("boolean"));
    assertEquals(true, result);
    result = AvroConverter.toAvro(TestUtils.jsonTree("false"), createPrimitiveSchema("boolean"));
    assertEquals(false, result);

    result = AvroConverter.toAvro(TestUtils.jsonTree("12"), createPrimitiveSchema("int"));
    assertTrue(result instanceof Integer);
    assertEquals(12, result);

    result = AvroConverter.toAvro(TestUtils.jsonTree("12"), createPrimitiveSchema("long"));
    assertTrue(result instanceof Long);
    assertEquals(12L, result);
    result = AvroConverter.toAvro(TestUtils.jsonTree("5000000000"), createPrimitiveSchema("long"));
    assertTrue(result instanceof Long);
    assertEquals(5000000000L, result);

    result = AvroConverter.toAvro(TestUtils.jsonTree("23.2"), createPrimitiveSchema("float"));
    assertTrue(result instanceof Float);
    assertEquals(23.2f, result);
    result = AvroConverter.toAvro(TestUtils.jsonTree("23"), createPrimitiveSchema("float"));
    assertTrue(result instanceof Float);
    assertEquals(23.0f, result);

    result = AvroConverter.toAvro(TestUtils.jsonTree("23.2"), createPrimitiveSchema("double"));
    assertTrue(result instanceof Double);
    assertEquals(23.2, result);
    result = AvroConverter.toAvro(TestUtils.jsonTree("23"), createPrimitiveSchema("double"));
    assertTrue(result instanceof Double);
    assertEquals(23.0, result);

    // We can test bytes simply using simple ASCII string since the translation is direct in that
    // case
    result = AvroConverter.toAvro(new TextNode("hello"), createPrimitiveSchema("bytes"));
    assertTrue(result instanceof ByteBuffer);
    assertEquals(EntityUtils.encodeBase64Binary("hello".getBytes()),
            EntityUtils.encodeBase64Binary(((ByteBuffer) result).array()));

    result = AvroConverter.toAvro(TestUtils.jsonTree("\"a string\""), createPrimitiveSchema("string"));
    assertTrue(result instanceof Utf8);
    assertEquals(new Utf8("a string"), result);
}

From source file:com.aol.one.patch.DefaultPatcherTest.java

@Test(expected = PatchException.class)
public void testAddForFailure() throws PatchException {
    List<PatchOperation> operations = new ArrayList<>();
    operations.add(new AddOperation("/child/someBlahField", new TextNode("test")));
    patcher.patch(testObject, operations);
}

From source file:org.walkmod.conf.providers.yml.AddConfigurationParameterYMLAction.java

@Override
public void doAction(JsonNode node) throws Exception {

    List<JsonNode> elementsToModify = new LinkedList<JsonNode>();

    if (node.has("chains")) {
        JsonNode aux = node.get("chains");
        if (aux.isArray()) {
            ArrayNode chainsList = (ArrayNode) node.get("chains");
            Iterator<JsonNode> it = chainsList.iterator();

            while (it.hasNext()) {
                JsonNode next = it.next();
                JsonNode walkerNode = null;
                JsonNode transformationsNode = null;
                if (chain == null || chain.equals(next.get("name").asText())) {
                    if (category != null) {
                        if (node.has(category)) { // reader, walker,
                            // writer
                            JsonNode categoryNode = node.get(category);
                            analyzeNode(categoryNode, type, name, elementsToModify);
                        } else {
                            if (category.equals("transformation")) {
                                if (node.has("transformations")) {
                                    transformationsNode = node.get("transformations");
                                }/*from  w  ww. ja va  2s.  c  o m*/
                            }
                            if (node.has("walker")) {
                                walkerNode = node.get("walker");
                            }
                        }
                    } else {
                        Iterator<JsonNode> it2 = next.iterator();
                        while (it2.hasNext()) {
                            analyzeNode(it2.next(), type, name, elementsToModify);
                        }
                        if (next.has("walker")) {
                            walkerNode = next.get("walker");

                        } else if (next.has("transformations")) {
                            transformationsNode = next.get("transformations");
                        }
                    }
                }
                if (walkerNode != null) {
                    if (category != null) {

                        if (walkerNode.has(category)) {
                            JsonNode categoryNode = node.get(category);
                            analyzeNode(categoryNode, type, name, elementsToModify);
                        }
                    } else if (walkerNode.has("transformations")) {
                        transformationsNode = walkerNode.get("transformations");

                    }

                }
                if (transformationsNode != null) {
                    if (transformationsNode.isArray()) {
                        ArrayNode transformationsArray = (ArrayNode) transformationsNode;
                        Iterator<JsonNode> it2 = transformationsArray.iterator();

                        while (it2.hasNext()) {
                            JsonNode current = it2.next();

                            analyzeNode(current, type, name, elementsToModify);
                        }
                    }
                }
            }
        }
    } else if (node.has("transformations") && (category == null || "transformation".equals(category))) {

        JsonNode transformationsNode = node.get("transformations");
        if (transformationsNode.isArray()) {
            ArrayNode transformationsArray = (ArrayNode) transformationsNode;
            Iterator<JsonNode> it = transformationsArray.iterator();

            while (it.hasNext()) {
                JsonNode current = it.next();
                analyzeNode(current, type, name, elementsToModify);
            }
        }

    }

    Iterator<JsonNode> it = elementsToModify.iterator();
    while (it.hasNext()) {
        JsonNode current = it.next();
        if (current.isObject()) {
            JsonNode params = null;
            if (current.has("params")) {
                params = current.get("params");
                if (params.isObject()) {
                    ((ObjectNode) params).set(param, new TextNode(value));
                }
            } else {
                Map<String, Object> paramToAdd = new HashMap<String, Object>();
                paramToAdd.put(param, value);
                populateParams((ObjectNode) current, paramToAdd);
            }
        }
    }

    if (!elementsToModify.isEmpty()) {
        provider.write(node);
    }

}