001// Generated by delombok at Thu Mar 19 15:24:58 GMT 2020 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020package org.apache.isis.viewer.restfulobjects.rendering.domainobjects; 021 022import java.math.BigDecimal; 023import java.math.BigInteger; 024import java.util.Arrays; 025import java.util.List; 026import java.util.function.Function; 027import org.joda.time.DateTime; 028import org.joda.time.LocalDate; 029import org.joda.time.LocalDateTime; 030import org.joda.time.format.DateTimeFormat; 031import org.joda.time.format.DateTimeFormatter; 032import org.joda.time.format.ISODateTimeFormat; 033import org.apache.isis.core.commons.internal.collections._Lists; 034import org.apache.isis.core.metamodel.spec.ManagedObject; 035import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation; 036import org.apache.isis.viewer.restfulobjects.rendering.domainobjects.JsonValueEncoder.JsonValueConverter; 037 038/** 039 * Similar to Isis' value encoding, but with additional support for JSON 040 041 * primitives. 042 */ 043public final class JsonValueEncoder_Converters { 044 public List<JsonValueConverter> asList(Function<Object, ManagedObject> pojoToAdapter) { 045 final java.util.ArrayList<org.apache.isis.viewer.restfulobjects.rendering.domainobjects.JsonValueEncoder.JsonValueConverter> converters = _Lists.<JsonValueConverter>newArrayList(); 046 converters.add(new JsonValueConverter(null, "string", String.class) { 047 @Override 048 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 049 if (repr.isString()) { 050 return pojoToAdapter.apply(repr.asString()); 051 } 052 return null; 053 } 054 @Override 055 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 056 final java.lang.Object obj = unwrapAsObjectElseNullNode(objectAdapter); 057 if (obj instanceof String) { 058 repr.mapPut("value", (String) obj); 059 } else { 060 repr.mapPut("value", obj); 061 } 062 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 063 return obj; 064 } 065 }); 066 converters.add(new JsonValueConverter(null, "boolean", boolean.class, Boolean.class) { 067 @Override 068 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 069 if (repr.isBoolean()) { 070 return pojoToAdapter.apply(repr.asBoolean()); 071 } 072 return null; 073 } 074 @Override 075 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 076 final java.lang.Object obj = unwrapAsObjectElseNullNode(objectAdapter); 077 if (obj instanceof Boolean) { 078 repr.mapPut("value", (Boolean) obj); 079 } else { 080 repr.mapPut("value", obj); 081 } 082 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 083 return obj; 084 } 085 }); 086 converters.add(new JsonValueConverter("int", "byte", byte.class, Byte.class) { 087 @Override 088 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 089 if (repr.isNumber()) { 090 return pojoToAdapter.apply(repr.asNumber().byteValue()); 091 } 092 if (repr.isInt()) { 093 return pojoToAdapter.apply((byte) (int) repr.asInt()); 094 } 095 if (repr.isLong()) { 096 return pojoToAdapter.apply((byte) (long) repr.asLong()); 097 } 098 if (repr.isBigInteger()) { 099 return pojoToAdapter.apply(repr.asBigInteger().byteValue()); 100 } 101 return null; 102 } 103 @Override 104 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 105 final java.lang.Object obj = unwrapAsObjectElseNullNode(objectAdapter); 106 if (obj instanceof Byte) { 107 repr.mapPut("value", (Byte) obj); 108 } else { 109 repr.mapPut("value", obj); 110 } 111 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 112 return obj; 113 } 114 }); 115 converters.add(new JsonValueConverter("int", "short", short.class, Short.class) { 116 @Override 117 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 118 if (repr.isNumber()) { 119 return pojoToAdapter.apply(repr.asNumber().shortValue()); 120 } 121 if (repr.isInt()) { 122 return pojoToAdapter.apply((short) (int) repr.asInt()); 123 } 124 if (repr.isLong()) { 125 return pojoToAdapter.apply((short) (long) repr.asLong()); 126 } 127 if (repr.isBigInteger()) { 128 return pojoToAdapter.apply(repr.asBigInteger().shortValue()); 129 } 130 return null; 131 } 132 @Override 133 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 134 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 135 if (obj instanceof Short) { 136 repr.mapPut("value", (Short) obj); 137 } else { 138 repr.mapPut("value", obj); 139 } 140 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 141 return obj; 142 } 143 }); 144 converters.add(new JsonValueConverter("int", "int", int.class, Integer.class) { 145 @Override 146 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 147 if (repr.isInt()) { 148 return pojoToAdapter.apply(repr.asInt()); 149 } 150 if (repr.isLong()) { 151 return pojoToAdapter.apply(repr.asLong().intValue()); 152 } 153 if (repr.isBigInteger()) { 154 return pojoToAdapter.apply(repr.asBigInteger().intValue()); 155 } 156 if (repr.isNumber()) { 157 return pojoToAdapter.apply(repr.asNumber().intValue()); 158 } 159 return null; 160 } 161 @Override 162 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 163 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 164 if (obj instanceof Integer) { 165 repr.mapPut("value", (Integer) obj); 166 } else { 167 repr.mapPut("value", obj); 168 } 169 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 170 return obj; 171 } 172 }); 173 converters.add(new JsonValueConverter("int", "long", long.class, Long.class) { 174 @Override 175 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 176 if (repr.isLong()) { 177 return pojoToAdapter.apply(repr.asLong()); 178 } 179 if (repr.isInt()) { 180 return pojoToAdapter.apply(repr.asLong()); 181 } 182 if (repr.isBigInteger()) { 183 return pojoToAdapter.apply(repr.asBigInteger().longValue()); 184 } 185 if (repr.isNumber()) { 186 return pojoToAdapter.apply(repr.asNumber().longValue()); 187 } 188 return null; 189 } 190 @Override 191 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 192 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 193 if (obj instanceof Long) { 194 final Long l = (Long) obj; 195 repr.mapPut("value", l); 196 } else { 197 repr.mapPut("value", obj); 198 } 199 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 200 return obj; 201 } 202 }); 203 converters.add(new JsonValueConverter("decimal", "float", float.class, Float.class) { 204 @Override 205 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 206 if (repr.isDecimal()) { 207 return pojoToAdapter.apply(repr.asDouble().floatValue()); 208 } 209 if (repr.isNumber()) { 210 return pojoToAdapter.apply(repr.asNumber().floatValue()); 211 } 212 if (repr.isLong()) { 213 return pojoToAdapter.apply(repr.asLong().floatValue()); 214 } 215 if (repr.isInt()) { 216 return pojoToAdapter.apply(repr.asInt().floatValue()); 217 } 218 if (repr.isBigInteger()) { 219 return pojoToAdapter.apply(repr.asBigInteger().floatValue()); 220 } 221 return null; 222 } 223 @Override 224 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 225 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 226 if (obj instanceof Float) { 227 final Float f = (Float) obj; 228 repr.mapPut("value", f); 229 } else { 230 repr.mapPut("value", obj); 231 } 232 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 233 return obj; 234 } 235 }); 236 converters.add(new JsonValueConverter("decimal", "double", double.class, Double.class) { 237 @Override 238 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 239 if (repr.isDecimal()) { 240 return pojoToAdapter.apply(repr.asDouble()); 241 } 242 if (repr.isLong()) { 243 return pojoToAdapter.apply(repr.asLong().doubleValue()); 244 } 245 if (repr.isInt()) { 246 return pojoToAdapter.apply(repr.asInt().doubleValue()); 247 } 248 if (repr.isBigInteger()) { 249 return pojoToAdapter.apply(repr.asBigInteger().doubleValue()); 250 } 251 if (repr.isBigDecimal()) { 252 return pojoToAdapter.apply(repr.asBigDecimal().doubleValue()); 253 } 254 if (repr.isNumber()) { 255 return pojoToAdapter.apply(repr.asNumber().doubleValue()); 256 } 257 return null; 258 } 259 @Override 260 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 261 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 262 if (obj instanceof Double) { 263 final Double d = (Double) obj; 264 repr.mapPut("value", d); 265 } else { 266 repr.mapPut("value", obj); 267 } 268 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 269 return obj; 270 } 271 }); 272 converters.add(new JsonValueConverter(null, "char", char.class, Character.class) { 273 @Override 274 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 275 if (repr.isString()) { 276 final String str = repr.asString(); 277 if (str != null && str.length() > 0) { 278 return pojoToAdapter.apply(str.charAt(0)); 279 } 280 } 281 // in case a char literal was provided 282 if (repr.isInt()) { 283 final Integer x = repr.asInt(); 284 if (Character.MIN_VALUE <= x && x <= Character.MAX_VALUE) { 285 char c = (char) x.intValue(); 286 return pojoToAdapter.apply(c); 287 } 288 } 289 return null; 290 } 291 @Override 292 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 293 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 294 if (obj instanceof Character) { 295 final Character c = (Character) obj; 296 repr.mapPut("value", c); 297 } else { 298 repr.mapPut("value", obj); 299 } 300 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 301 return obj; 302 } 303 }); 304 converters.add(new JsonValueConverter("big-integer(18)", "javamathbiginteger", BigInteger.class) { 305 @Override 306 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 307 if (repr.isString()) { 308 return pojoToAdapter.apply(new BigInteger(repr.asString())); 309 } 310 if (repr.isBigInteger()) { 311 return pojoToAdapter.apply(repr.asBigInteger(format)); 312 } 313 if (repr.isLong()) { 314 return pojoToAdapter.apply(BigInteger.valueOf(repr.asLong())); 315 } 316 if (repr.isInt()) { 317 return pojoToAdapter.apply(BigInteger.valueOf(repr.asInt())); 318 } 319 if (repr.isNumber()) { 320 return pojoToAdapter.apply(BigInteger.valueOf(repr.asNumber().longValue())); 321 } 322 return null; 323 } 324 @Override 325 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 326 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 327 if (obj instanceof BigInteger) { 328 final BigInteger bi = (BigInteger) obj; 329 repr.mapPut("value", bi); 330 } else { 331 repr.mapPut("value", obj); 332 } 333 appendFormats(repr, format != null ? format : this.format, xIsisFormat, suppressExtensions); 334 return obj; 335 } 336 }); 337 converters.add(new JsonValueConverter("big-decimal", "javamathbigdecimal", BigDecimal.class) { 338 @Override 339 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 340 if (repr.isString()) { 341 return pojoToAdapter.apply(new BigDecimal(repr.asString())); 342 } 343 if (repr.isBigDecimal()) { 344 return pojoToAdapter.apply(repr.asBigDecimal(format)); 345 } 346 if (repr.isBigInteger()) { 347 return pojoToAdapter.apply(new BigDecimal(repr.asBigInteger())); 348 } 349 if (repr.isDecimal()) { 350 return pojoToAdapter.apply(BigDecimal.valueOf(repr.asDouble())); 351 } 352 if (repr.isLong()) { 353 return pojoToAdapter.apply(BigDecimal.valueOf(repr.asLong())); 354 } 355 if (repr.isInt()) { 356 return pojoToAdapter.apply(BigDecimal.valueOf(repr.asInt())); 357 } 358 return null; 359 } 360 @Override 361 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 362 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 363 if (obj instanceof BigDecimal) { 364 repr.mapPut("value", (BigDecimal) obj); 365 } else { 366 repr.mapPut("value", obj); 367 } 368 appendFormats(repr, format != null ? format : this.format, xIsisFormat, suppressExtensions); 369 return obj; 370 } 371 }); 372 converters.add(new JsonValueConverter("date", "jodalocaldate", LocalDate.class) { 373 // these formatters do NOT use withZoneUTC() 374 final List<DateTimeFormatter> formatters = Arrays.asList(ISODateTimeFormat.date(), ISODateTimeFormat.basicDate(), DateTimeFormat.forPattern("yyyyMMdd"), JsonRepresentation.yyyyMMdd); 375 @Override 376 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 377 if (repr.isString()) { 378 final String dateStr = repr.asString(); 379 for (DateTimeFormatter formatter : formatters) { 380 try { 381 final LocalDate parsedDate = formatter.parseLocalDate(dateStr); 382 return pojoToAdapter.apply(parsedDate); 383 } catch (IllegalArgumentException ex) { 384 } 385 // fall through 386 } 387 } 388 return null; 389 } 390 @Override 391 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 392 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 393 if (obj instanceof LocalDate) { 394 final LocalDate date = (LocalDate) obj; 395 final String dateStr = formatters.get(0).print(date.toDateTimeAtStartOfDay()); 396 repr.mapPut("value", dateStr); 397 } else { 398 repr.mapPut("value", obj); 399 } 400 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 401 return obj; 402 } 403 }); 404 converters.add(new JsonValueConverter("date-time", "jodalocaldatetime", LocalDateTime.class) { 405 final List<DateTimeFormatter> formatters = Arrays.asList(ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(), ISODateTimeFormat.dateTime().withZoneUTC(), ISODateTimeFormat.basicDateTimeNoMillis().withZoneUTC(), ISODateTimeFormat.basicDateTime().withZoneUTC(), JsonRepresentation.yyyyMMddTHHmmssZ.withZoneUTC()); 406 @Override 407 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 408 if (repr.isString()) { 409 final String dateStr = repr.asString(); 410 for (DateTimeFormatter formatter : formatters) { 411 try { 412 final LocalDateTime parsedDate = formatter.parseLocalDateTime(dateStr); 413 return pojoToAdapter.apply(parsedDate); 414 } catch (IllegalArgumentException ex) { 415 } 416 // fall through 417 } 418 } 419 return null; 420 } 421 @Override 422 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 423 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 424 if (obj instanceof LocalDateTime) { 425 final LocalDateTime date = (LocalDateTime) obj; 426 final String dateStr = formatters.get(0).print(date.toDateTime()); 427 repr.mapPut("value", dateStr); 428 } else { 429 repr.mapPut("value", obj); 430 } 431 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 432 return obj; 433 } 434 }); 435 converters.add(new JsonValueConverter("date-time", "jodadatetime", DateTime.class) { 436 final List<DateTimeFormatter> formatters = Arrays.asList(ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(), ISODateTimeFormat.dateTime().withZoneUTC(), ISODateTimeFormat.basicDateTimeNoMillis().withZoneUTC(), ISODateTimeFormat.basicDateTime().withZoneUTC(), JsonRepresentation.yyyyMMddTHHmmssZ.withZoneUTC()); 437 @Override 438 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 439 if (repr.isString()) { 440 final String dateStr = repr.asString(); 441 for (DateTimeFormatter formatter : formatters) { 442 try { 443 final DateTime parsedDate = formatter.parseDateTime(dateStr); 444 return pojoToAdapter.apply(parsedDate); 445 } catch (IllegalArgumentException ex) { 446 } 447 // fall through 448 } 449 } 450 return null; 451 } 452 @Override 453 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 454 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 455 if (obj instanceof DateTime) { 456 final DateTime date = (DateTime) obj; 457 final String dateStr = formatters.get(0).print(date.toDateTime()); 458 repr.mapPut("value", dateStr); 459 } else { 460 repr.mapPut("value", obj); 461 } 462 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 463 return obj; 464 } 465 }); 466 converters.add(new JsonValueConverter("date-time", "javautildate", java.util.Date.class) { 467 final List<DateTimeFormatter> formatters = Arrays.asList(ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(), ISODateTimeFormat.dateTime().withZoneUTC(), ISODateTimeFormat.basicDateTimeNoMillis().withZoneUTC(), ISODateTimeFormat.basicDateTime().withZoneUTC(), JsonRepresentation.yyyyMMddTHHmmssZ.withZoneUTC()); 468 @Override 469 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 470 if (repr.isString()) { 471 final String dateStr = repr.asString(); 472 for (DateTimeFormatter formatter : formatters) { 473 try { 474 final DateTime parseDateTime = formatter.parseDateTime(dateStr); 475 final java.util.Date parsedDate = parseDateTime.toDate(); 476 return pojoToAdapter.apply(parsedDate); 477 } catch (IllegalArgumentException ex) { 478 } 479 // fall through 480 } 481 } 482 return null; 483 } 484 @Override 485 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 486 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 487 if (obj instanceof java.util.Date) { 488 final java.util.Date date = (java.util.Date) obj; 489 final DateTimeFormatter dateTimeFormatter = formatters.get(0); 490 final String dateStr = dateTimeFormatter.print(new DateTime(date)); 491 repr.mapPut("value", dateStr); 492 } else { 493 repr.mapPut("value", obj); 494 } 495 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 496 return obj; 497 } 498 }); 499 converters.add(new JsonValueConverter("date", "javasqldate", java.sql.Date.class) { 500 final List<DateTimeFormatter> formatters = Arrays.asList(ISODateTimeFormat.date().withZoneUTC(), ISODateTimeFormat.basicDate().withZoneUTC(), JsonRepresentation.yyyyMMdd.withZoneUTC()); 501 @Override 502 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 503 if (repr.isString()) { 504 final String dateStr = repr.asString(); 505 for (DateTimeFormatter formatter : formatters) { 506 try { 507 final DateTime parseDateTime = formatter.parseDateTime(dateStr); 508 final java.sql.Date parsedDate = new java.sql.Date(parseDateTime.getMillis()); 509 return pojoToAdapter.apply(parsedDate); 510 } catch (IllegalArgumentException ex) { 511 } 512 // fall through 513 } 514 } 515 return null; 516 } 517 @Override 518 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 519 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 520 if (obj instanceof java.sql.Date) { 521 final java.sql.Date date = (java.sql.Date) obj; 522 final String dateStr = formatters.get(0).print(new DateTime(date)); 523 repr.mapPut("value", dateStr); 524 } else { 525 repr.mapPut("value", obj); 526 } 527 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 528 return obj; 529 } 530 }); 531 converters.add(new JsonValueConverter("time", "javasqltime", java.sql.Time.class) { 532 final List<DateTimeFormatter> formatters = Arrays.asList(ISODateTimeFormat.hourMinuteSecond().withZoneUTC(), ISODateTimeFormat.basicTimeNoMillis().withZoneUTC(), ISODateTimeFormat.basicTime().withZoneUTC(), JsonRepresentation._HHmmss.withZoneUTC()); 533 @Override 534 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 535 if (repr.isString()) { 536 final String dateStr = repr.asString(); 537 for (DateTimeFormatter formatter : formatters) { 538 try { 539 final DateTime parseDateTime = formatter.parseDateTime(dateStr); 540 final java.sql.Time parsedTime = new java.sql.Time(parseDateTime.getMillis()); 541 return pojoToAdapter.apply(parsedTime); 542 } catch (IllegalArgumentException ex) { 543 } 544 // fall through 545 } 546 } 547 return null; 548 } 549 @Override 550 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 551 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 552 if (obj instanceof java.sql.Time) { 553 final java.sql.Time date = (java.sql.Time) obj; 554 final String dateStr = formatters.get(0).print(new DateTime(date)); 555 repr.mapPut("value", dateStr); 556 } else { 557 repr.mapPut("value", obj); 558 } 559 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 560 return obj; 561 } 562 }); 563 converters.add(new JsonValueConverter("utc-millisec", "javasqltimestamp", java.sql.Timestamp.class) { 564 @Override 565 public ManagedObject asAdapter(JsonRepresentation repr, String format) { 566 if (repr.isLong()) { 567 final Long millis = repr.asLong(); 568 final java.sql.Timestamp parsedTimestamp = new java.sql.Timestamp(millis); 569 return pojoToAdapter.apply(parsedTimestamp); 570 } 571 if (repr.isString()) { 572 final String dateStr = repr.asString(); 573 try { 574 final Long parseMillis = Long.parseLong(dateStr); 575 final java.sql.Timestamp parsedTimestamp = new java.sql.Timestamp(parseMillis); 576 return pojoToAdapter.apply(parsedTimestamp); 577 } catch (IllegalArgumentException ex) { 578 } 579 } 580 // fall through 581 return null; 582 } 583 @Override 584 public Object appendValueAndFormat(ManagedObject objectAdapter, String format, JsonRepresentation repr, boolean suppressExtensions) { 585 final Object obj = unwrapAsObjectElseNullNode(objectAdapter); 586 if (obj instanceof java.sql.Timestamp) { 587 final java.sql.Timestamp date = (java.sql.Timestamp) obj; 588 final long millisStr = date.getTime(); 589 repr.mapPut("value", millisStr); 590 } else { 591 repr.mapPut("value", obj); 592 } 593 appendFormats(repr, this.format, xIsisFormat, suppressExtensions); 594 return obj; 595 } 596 }); 597 return converters; 598 } 599 600 static void appendFormats(JsonRepresentation repr, String format, String xIsisFormat, boolean suppressExtensions) { 601 JsonValueEncoder.appendFormats(repr, format, xIsisFormat, suppressExtensions); 602 } 603 604 static Object unwrapAsObjectElseNullNode(ManagedObject adapter) { 605 return JsonValueEncoder.unwrapAsObjectElseNullNode(adapter); 606 } 607}