HeadArraySchema extends AbstractSchema { @Override public IJsonNode recordToJson(final PactRecord record, IJsonNode target) { if (this.getHeadSize() + 1 != record.getNumFields()) throw new IllegalStateException("Schema does not match to record!"); if (target == null) target = new ArrayNode(); else ((IArrayNode) target).clear(); for (int i = 0; i < this.getHeadSize(); i++) if (record.getField(i, JsonNodeWrapper.class) != null) ((IArrayNode) target).add(SopremoUtil.unwrap(record.getField(i, JsonNodeWrapper.class))); ((IArrayNode) target).addAll((IArrayNode) SopremoUtil.unwrap(record.getField(this.getHeadSize(), JsonNodeWrapper.class))); return target; } HeadArraySchema(final int headSize); int getHeadSize(); @Override IntSet indicesOf(final EvaluationExpression expression); @Override PactRecord jsonToRecord(final IJsonNode value, PactRecord target, final EvaluationContext context); @Override IJsonNode recordToJson(final PactRecord record, IJsonNode target);  }
@Test public void shouldConvertFromRecordToJson() { final PactRecord record = new PactRecord(); final HeadArraySchema schema = new HeadArraySchema(2); record.setField(0, SopremoUtil.wrap(IntNode.valueOf(0))); record.setField(1, SopremoUtil.wrap(IntNode.valueOf(1))); record.setField(2, SopremoUtil.wrap(new ArrayNode(IntNode.valueOf(2)))); final IArrayNode expected = new ArrayNode(IntNode.valueOf(0), IntNode.valueOf(1), IntNode.valueOf(2)); final IJsonNode result = schema.recordToJson(record, null); Assert.assertEquals(expected, result); }