1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.core.jsoniter
21 
22 import java.time.ZonedDateTime
23 import cats.data.{NonEmptyList => Nel}
24 import com.github.plokhotnyuk.jsoniter_scala.macros._
25 import com.github.plokhotnyuk.jsoniter_scala.core._
26 import eu.timepit.refined.refineV
27 import eu.timepit.refined.api.Refined
28 import eu.timepit.refined.collection.NonEmpty
29 import org.make.core.DateFormatters
30 import org.make.core.technical.Multilingual
31 
32 @SuppressWarnings(Array("org.wartremover.warts.Null", "org.wartremover.warts.AsInstanceOf"))
33 trait JsoniterCodecs {
34 
35   implicit val stringCodec: JsonValueCodec[String] = JsonCodecMaker.makeWithRequiredCollectionFields
36   implicit val multilingualCodec: JsonValueCodec[Multilingual[String]] = Multilingual.makeCodec
37 
38   implicit val refinedNonEmptyCodec: JsonValueCodec[String Refined NonEmpty] =
39     new JsonValueCodec[String Refined NonEmpty] {
40 
41       def decodeValue(in: JsonReader, default: String Refined NonEmpty): String Refined NonEmpty =
42         refineV[NonEmpty](in.readString(null)).getOrElse(default)
43 
44       def encodeValue(str: String Refined NonEmpty, out: JsonWriter): Unit =
45         out.writeVal(str.value)
46 
47       def nullValue: String Refined NonEmpty = null.asInstanceOf[String Refined NonEmpty]
48     }
49 
50   implicit val multilingualNECodec: JsonValueCodec[Multilingual[String Refined NonEmpty]] = Multilingual.makeCodec
51 
52   implicit def nonEmptyListCodec[T: JsonValueCodec]: JsonValueCodec[Nel[T]] =
53     new JsonValueCodec[Nel[T]] {
54 
55       private val listCodec: JsonValueCodec[List[T]] =
56         JsonCodecMaker.makeWithRequiredCollectionFields[List[T]]
57 
58       def decodeValue(in: JsonReader, default: Nel[T]): Nel[T] =
59         Nel.fromListUnsafe(listCodec.decodeValue(in, null))
60 
61       def encodeValue(nel: Nel[T], out: JsonWriter): Unit =
62         listCodec.encodeValue(nel.toList, out)
63 
64       def nullValue: Nel[T] = null.asInstanceOf[Nel[T]]
65     }
66 
67   implicit val zonedDateTimeCodec: JsonValueCodec[ZonedDateTime] =
68     new JsonValueCodec[ZonedDateTime] {
69 
70       def decodeValue(in: JsonReader, default: ZonedDateTime): ZonedDateTime =
71         ZonedDateTime.from(DateFormatters.default.parse(in.readString(null)))
72 
73       def encodeValue(dateTime: ZonedDateTime, out: JsonWriter): Unit =
74         out.writeVal(DateFormatters.default.format(dateTime))
75 
76       def nullValue: ZonedDateTime = null.asInstanceOf[ZonedDateTime]
77     }
78 }
Line Stmt Id Pos Tree Symbol Tests Code
36 1797 1456 - 1456 Select org.make.core.jsoniter.JsoniterCodecs.stringCodec org.make.core.proposal.indexed.proposaltest JsoniterCodecs.this.stringCodec
36 5072 1443 - 1465 ApplyToImplicitArgs org.make.core.technical.Multilingual.makeCodec org.make.core.proposal.indexed.proposaltest org.make.core.technical.Multilingual.makeCodec[String](JsoniterCodecs.this.stringCodec)
39 456 1550 - 1553 Apply org.make.core.jsoniter.JsoniterCodecs.$anon.<init> org.make.core.proposal.indexed.proposaltest new $anon()
42 4098 1704 - 1761 Apply scala.util.Either.getOrElse org.make.core.proposal.indexed.proposaltest eu.timepit.refined.`package`.refineV[eu.timepit.refined.collection.NonEmpty].apply[String](in.readString(null))(boolean.this.Not.notValidate[String, eu.timepit.refined.collection.Empty, this.R](collection.this.Empty.emptyValidate[String](((s: String) => scala.Predef.wrapString(s))))).getOrElse[eu.timepit.refined.api.Refined[String,eu.timepit.refined.collection.NonEmpty]](default)
45 2170 1861 - 1870 Select eu.timepit.refined.api.Refined.value org.make.core.proposal.indexed.proposaltest str.value
45 5333 1848 - 1871 Apply com.github.plokhotnyuk.jsoniter_scala.core.JsonWriter.writeVal org.make.core.proposal.indexed.proposaltest out.writeVal(str.value)
47 1712 1920 - 1962 TypeApply scala.Any.asInstanceOf org.make.core.proposal.indexed.proposaltest null.asInstanceOf[eu.timepit.refined.api.Refined[String,eu.timepit.refined.collection.NonEmpty]]
47 3452 1920 - 1924 Literal <nosymbol> org.make.core.proposal.indexed.proposaltest null
50 3706 2075 - 2075 Select org.make.core.jsoniter.JsoniterCodecs.refinedNonEmptyCodec org.make.core.proposal.indexed.proposaltest JsoniterCodecs.this.refinedNonEmptyCodec
50 1726 2062 - 2084 ApplyToImplicitArgs org.make.core.technical.Multilingual.makeCodec org.make.core.proposal.indexed.proposaltest org.make.core.technical.Multilingual.makeCodec[eu.timepit.refined.api.Refined[String,eu.timepit.refined.collection.NonEmpty]](JsoniterCodecs.this.refinedNonEmptyCodec)
53 658 2168 - 2171 Apply org.make.core.jsoniter.JsoniterCodecs.$anon.<init> org.make.core.proposal.indexed.proposaltest new $anon()
59 4278 2392 - 2443 Apply cats.data.NonEmptyList.fromListUnsafe org.make.core.proposal.indexed.proposaltest cats.data.NonEmptyList.fromListUnsafe[T]($anon.this.listCodec.decodeValue(in, null))
59 5250 2411 - 2442 Apply com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec.decodeValue org.make.core.proposal.indexed.proposaltest $anon.this.listCodec.decodeValue(in, null)
62 5337 2513 - 2551 Apply com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec.encodeValue org.make.core.proposal.indexed.proposaltest $anon.this.listCodec.encodeValue(nel.toList, out)
62 2110 2535 - 2545 Select cats.data.NonEmptyList.toList org.make.core.proposal.indexed.proposaltest nel.toList
64 3379 2583 - 2587 Literal <nosymbol> org.make.core.proposal.indexed.proposaltest null
64 1636 2583 - 2608 TypeApply scala.Any.asInstanceOf org.make.core.proposal.indexed.proposaltest null.asInstanceOf[cats.data.NonEmptyList[T]]
68 1648 2687 - 2690 Apply org.make.core.jsoniter.JsoniterCodecs.$anon.<init> org.make.core.proposal.indexed.proposaltest new $anon()
71 3982 2859 - 2878 Apply com.github.plokhotnyuk.jsoniter_scala.core.JsonReader.readString org.make.core.proposal.indexed.proposaltest in.readString(null)
71 1733 2830 - 2879 Apply java.time.format.DateTimeFormatter.parse org.make.core.proposal.indexed.proposaltest org.make.core.DateFormatters.default.parse(in.readString(null))
71 5261 2811 - 2880 Apply java.time.ZonedDateTime.from org.make.core.proposal.indexed.proposaltest java.time.ZonedDateTime.from(org.make.core.DateFormatters.default.parse(in.readString(null)))
74 3297 2975 - 3014 Apply java.time.format.DateTimeFormatter.format org.make.core.proposal.indexed.proposaltest org.make.core.DateFormatters.default.format(dateTime)
74 2157 2962 - 3015 Apply com.github.plokhotnyuk.jsoniter_scala.core.JsonWriter.writeVal org.make.core.proposal.indexed.proposaltest out.writeVal(org.make.core.DateFormatters.default.format(dateTime))
76 5610 3054 - 3058 Literal <nosymbol> org.make.core.proposal.indexed.proposaltest null
76 3583 3054 - 3086 TypeApply scala.Any.asInstanceOf org.make.core.proposal.indexed.proposaltest null.asInstanceOf[java.time.ZonedDateTime]