List of usage examples for com.fasterxml.jackson.core JsonFactory createGenerator
public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException
From source file:squash.tools.FakeBookingCreator.java
public static void main(String[] args) throws IOException { int numberOfDays = 21; int numberOfCourts = 5; int maxCourtSpan = 5; int numberOfSlots = 16; int maxSlotSpan = 3; int minSurnameLength = 2; int maxSurnameLength = 20; int minBookingsPerDay = 0; int maxBookingsPerDay = 8; LocalDate startDate = LocalDate.of(2016, 7, 5); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); List<Booking> bookings = new ArrayList<>(); for (LocalDate date = startDate; date.isBefore(startDate.plusDays(numberOfDays)); date = date.plusDays(1)) { int numBookings = ThreadLocalRandom.current().nextInt(minBookingsPerDay, maxBookingsPerDay + 1); List<Booking> daysBookings = new ArrayList<>(); for (int bookingIndex = 0; bookingIndex < numBookings; bookingIndex++) { String player1 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic( ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1)); String player2 = RandomStringUtils.randomAlphabetic(1) + "." + RandomStringUtils.randomAlphabetic( ThreadLocalRandom.current().nextInt(minSurnameLength, maxSurnameLength + 1)); Set<ImmutablePair<Integer, Integer>> bookedCourts = new HashSet<>(); daysBookings.forEach((booking) -> { addBookingToSet(booking, bookedCourts); });//from w w w.j a v a2 s . c o m Booking booking; Set<ImmutablePair<Integer, Integer>> courtsToBook = new HashSet<>(); do { // Loop until we create a booking of free courts int court = ThreadLocalRandom.current().nextInt(1, numberOfCourts + 1); int courtSpan = ThreadLocalRandom.current().nextInt(1, Math.min(maxCourtSpan + 1, numberOfCourts - court + 2)); int slot = ThreadLocalRandom.current().nextInt(1, numberOfSlots + 1); int slotSpan = ThreadLocalRandom.current().nextInt(1, Math.min(maxSlotSpan + 1, numberOfSlots - slot + 2)); booking = new Booking(court, courtSpan, slot, slotSpan, player1 + "/" + player2); booking.setDate(date.format(formatter)); courtsToBook.clear(); addBookingToSet(booking, courtsToBook); } while (Boolean.valueOf(Sets.intersection(courtsToBook, bookedCourts).size() > 0)); daysBookings.add(booking); } bookings.addAll(daysBookings); } // Encode bookings as JSON // Create the node factory that gives us nodes. JsonNodeFactory factory = new JsonNodeFactory(false); // Create a json factory to write the treenode as json. JsonFactory jsonFactory = new JsonFactory(); ObjectNode rootNode = factory.objectNode(); ArrayNode bookingsNode = rootNode.putArray("bookings"); for (int i = 0; i < bookings.size(); i++) { Booking booking = bookings.get(i); ObjectNode bookingNode = factory.objectNode(); bookingNode.put("court", booking.getCourt()); bookingNode.put("courtSpan", booking.getCourtSpan()); bookingNode.put("slot", booking.getSlot()); bookingNode.put("slotSpan", booking.getSlotSpan()); bookingNode.put("name", booking.getName()); bookingNode.put("date", booking.getDate()); bookingsNode.add(bookingNode); } // Add empty booking rules array - just so restore works rootNode.putArray("bookingRules"); rootNode.put("clearBeforeRestore", true); try (JsonGenerator generator = jsonFactory.createGenerator(new File("FakeBookings.json"), JsonEncoding.UTF8)) { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_EMPTY); mapper.setSerializationInclusion(Include.NON_NULL); mapper.writeTree(generator, rootNode); } }
From source file:com.rdg.json.util.JSONUtil.java
/** * transform object to xml/*from ww w .j a v a 2 s . co m*/ * * @param object * @param FILE_NAME */ public static void objectToJSON(Object object, String FILE_NAME) { JsonFactory factory = objectMapper.getJsonFactory(); try { File file = new File(FILE_NAME); JsonGenerator gen = factory.createGenerator(file, JsonEncoding.UTF8); objectMapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer()); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE); objectMapper.writeValue(gen, object); } catch (IOException e) { logger.error(InternationalizationManager.getString(Const.ERROR_IN_JSON_WRITE) + e); } }
From source file:jvmoptions.OptionAnalyzer.java
static Path toJson(String java6, String java7, String java8) throws Exception { Map<String, Map<String, String>> map6 = makeMap(java6); Map<String, Map<String, String>> map7 = makeMap(java7); Map<String, Map<String, String>> map8 = makeMap(java8); Path output = Paths.get("result", filename("json")); JsonFactory factory = new JsonFactory(); JsonGenerator jg = factory.createGenerator(output.toFile(), JsonEncoding.UTF8).useDefaultPrettyPrinter(); jg.writeStartObject();// w w w . j ava 2 s . com Stream.of(map6, map7, map8).map(Map::keySet).flatMap(Collection::stream).sorted().distinct().forEach(k -> { try { jg.writeFieldName(k); jg.writeStartObject(); Map<String, String> base = pick(k, map8, map7, map6); jg.writeStringField("kind", base.get("kind")); jg.writeStringField("type", base.get("type")); jg.writeStringField("description", base.get("description")); jg.writeStringField("file", base.get("file")); write(jg, "java6", map6.get(k)); write(jg, "java7", map7.get(k)); write(jg, "java8", map8.get(k)); jg.writeEndObject(); } catch (IOException e) { throw new UncheckedIOException(e); } }); jg.writeEndObject(); jg.close(); return output; }
From source file:com.cedarsoft.serialization.jackson.NullSerializerTest.java
@Test public void testIt() throws Exception { JsonFactory jsonFactory = JacksonSupport.getJsonFactory(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); NullSerializer serializer = getSerializer(); serializer.serialize(generator, null, Version.valueOf(1, 0, 0)); generator.close();/*from ww w . j av a2s. c o m*/ JsonUtils.assertJsonEquals("null", out.toString()); assertNull(serializer.deserialize(new ByteArrayInputStream(out.toByteArray()))); }
From source file:org.dswarm.xsd2jsonschema.model.JSRoot.java
public void render(final JsonFactory jsonFactory, final File file, final JsonEncoding encoding) throws IOException { render(jsonFactory.createGenerator(file, encoding)); }
From source file:org.dswarm.xsd2jsonschema.model.JSRoot.java
public void render(final JsonFactory jsonFactory, final OutputStream out, final JsonEncoding encoding) throws IOException { render(jsonFactory.createGenerator(out, encoding)); }
From source file:com.cedarsoft.serialization.jackson.IntegerSerializerTest.java
@Test public void testIt() throws Exception { JsonFactory jsonFactory = JacksonSupport.getJsonFactory(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); getSerializer().serialize(generator, 12, Version.valueOf(1, 0, 0)); generator.close();//from w w w . jav a2 s. co m JsonUtils.assertJsonEquals("12", out.toString()); }
From source file:com.cedarsoft.serialization.jackson.StringSerializerTest.java
License:asdf
@Test public void testIt() throws Exception { JsonFactory jsonFactory = JacksonSupport.getJsonFactory(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); getSerializer().serialize(generator, "asdf", Version.valueOf(1, 0, 0)); generator.close();//from w w w . j a v a 2s . c o m JsonUtils.assertJsonEquals("\"asdf\"", out.toString()); }
From source file:com.cedarsoft.serialization.jackson.ListSerializerTest.java
@Test public void testIt() throws Exception { JsonFactory jsonFactory = JacksonSupport.getJsonFactory(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); getSerializer().serialize(generator, Arrays.asList("a", "b", "c", 42, 3.141, false, null), Version.valueOf(1, 0, 0));// w ww. j av a 2s.com generator.close(); JsonUtils.assertJsonEquals("[ \"a\", \"b\", \"c\", 42, 3.141, false, null ]", out.toString()); List<? extends Object> deserialized = getSerializer() .deserialize(new ByteArrayInputStream(out.toByteArray())); assertEquals(7, deserialized.size()); assertEquals("a", deserialized.get(0)); assertEquals("b", deserialized.get(1)); assertEquals("c", deserialized.get(2)); assertEquals(42, deserialized.get(3)); assertEquals(3.141, deserialized.get(4)); assertEquals(false, deserialized.get(5)); assertEquals(null, deserialized.get(6)); }
From source file:com.cedarsoft.serialization.jackson.NumberSerializerTest.java
@Test public void testDouble() throws Exception { JsonFactory jsonFactory = JacksonSupport.getJsonFactory(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8); getSerializer().serialize(generator, 11133.4, Version.valueOf(1, 0, 0)); generator.close();/*from ww w . j av a2 s . co m*/ JsonUtils.assertJsonEquals("11133.4", out.toString()); }