@@ -976,30 +976,63 @@ private[smithy4s] class SchemaVisitorJCodec(
976
976
977
977
private type Writer [A ] = A => JsonWriter => Unit
978
978
979
- private def taggedUnion [U ](
980
- alternatives : Vector [Alt [U , _]]
981
- )(dispatch : Alt .Dispatcher [U ]): JCodec [U ] =
982
- new JCodec [U ] {
983
- val expecting : String = " tagged-union"
979
+ private abstract class TaggedUnionJCodec [U ](alternatives : Vector [Alt [U , _]])(
980
+ dispatch : Alt .Dispatcher [U ]
981
+ ) extends JCodec [U ] {
984
982
985
- override def canBeKey : Boolean = false
983
+ val expecting = " tagged-union "
986
984
987
- def jsonLabel [A ](alt : Alt [U , A ]): String =
988
- alt.hints.get(JsonName ) match {
989
- case None => alt.label
990
- case Some (x) => x.value
985
+ override def canBeKey : Boolean = false
986
+
987
+ def jsonLabel [A ](alt : Alt [U , A ]): String =
988
+ alt.hints.get(JsonName ) match {
989
+ case None => alt.label
990
+ case Some (x) => x.value
991
+ }
992
+
993
+ protected val handlerMap =
994
+ new util.HashMap [String , (Cursor , JsonReader ) => U ] {
995
+ def handler [A ](alt : Alt [U , A ]) = {
996
+ val codec = apply(alt.schema)
997
+ (cursor : Cursor , reader : JsonReader ) =>
998
+ alt.inject(cursor.decode(codec, reader))
991
999
}
992
1000
993
- private [this ] val handlerMap =
994
- new util.HashMap [String , (Cursor , JsonReader ) => U ] {
995
- def handler [A ](alt : Alt [U , A ]) = {
996
- val codec = apply(alt.schema)
997
- (cursor : Cursor , reader : JsonReader ) =>
998
- alt.inject(cursor.decode(codec, reader))
1001
+ alternatives.foreach(alt => put(jsonLabel(alt), handler(alt)))
1002
+ }
1003
+
1004
+ protected val precompiler = new smithy4s.schema.Alt .Precompiler [Writer ] {
1005
+ def apply [A ](label : String , instance : Schema [A ]): Writer [A ] = {
1006
+ val jsonLabel =
1007
+ instance.hints.get(JsonName ).map(_.value).getOrElse(label)
1008
+ val jcodecA = instance.compile(self)
1009
+ a =>
1010
+ out => {
1011
+ out.writeObjectStart()
1012
+ out.writeKey(jsonLabel)
1013
+ jcodecA.encodeValue(a, out)
1014
+ out.writeObjectEnd()
999
1015
}
1016
+ }
1017
+ }
1018
+ protected val writer = dispatch.compile(precompiler)
1000
1019
1001
- alternatives.foreach(alt => put(jsonLabel(alt), handler(alt)))
1002
- }
1020
+ def encodeValue (u : U , out : JsonWriter ): Unit = {
1021
+ writer(u)(out)
1022
+ }
1023
+
1024
+ def decodeKey (in : JsonReader ): U =
1025
+ in.decodeError(" Cannot use coproducts as keys" )
1026
+
1027
+ def encodeKey (u : U , out : JsonWriter ): Unit =
1028
+ out.encodeError(" Cannot use coproducts as keys" )
1029
+
1030
+ }
1031
+
1032
+ private def taggedUnion [U ](
1033
+ alternatives : Vector [Alt [U , _]]
1034
+ )(dispatch : Alt .Dispatcher [U ]): JCodec [U ] =
1035
+ new TaggedUnionJCodec [U ](alternatives)(dispatch) {
1003
1036
1004
1037
def decodeValue (cursor : Cursor , in : JsonReader ): U =
1005
1038
if (in.isNextToken('{' )) {
@@ -1020,66 +1053,20 @@ private[smithy4s] class SchemaVisitorJCodec(
1020
1053
}
1021
1054
}
1022
1055
} else in.decodeError(" Expected JSON object" )
1023
-
1024
- val precompiler = new smithy4s.schema.Alt .Precompiler [Writer ] {
1025
- def apply [A ](label : String , instance : Schema [A ]): Writer [A ] = {
1026
- val jsonLabel =
1027
- instance.hints.get(JsonName ).map(_.value).getOrElse(label)
1028
- val jcodecA = instance.compile(self)
1029
- a =>
1030
- out => {
1031
- out.writeObjectStart()
1032
- out.writeKey(jsonLabel)
1033
- jcodecA.encodeValue(a, out)
1034
- out.writeObjectEnd()
1035
- }
1036
- }
1037
- }
1038
- val writer = dispatch.compile(precompiler)
1039
-
1040
- def encodeValue (u : U , out : JsonWriter ): Unit = {
1041
- writer(u)(out)
1042
- }
1043
-
1044
- def decodeKey (in : JsonReader ): U =
1045
- in.decodeError(" Cannot use coproducts as keys" )
1046
-
1047
- def encodeKey (u : U , out : JsonWriter ): Unit =
1048
- out.encodeError(" Cannot use coproducts as keys" )
1049
1056
}
1050
1057
1051
1058
private def lenientTaggedUnion [U ](
1052
1059
alternatives : Vector [Alt [U , _]]
1053
1060
)(dispatch : Alt .Dispatcher [U ]): JCodec [U ] =
1054
- new JCodec [U ] {
1055
- val expecting : String = " tagged-union"
1056
-
1057
- override def canBeKey : Boolean = false
1058
-
1059
- def jsonLabel [A ](alt : Alt [U , A ]): String =
1060
- alt.hints.get(JsonName ) match {
1061
- case None => alt.label
1062
- case Some (x) => x.value
1063
- }
1064
-
1065
- private [this ] val handlerMap =
1066
- new util.HashMap [String , (Cursor , JsonReader ) => U ] {
1067
- def handler [A ](alt : Alt [U , A ]) = {
1068
- val codec = apply(alt.schema)
1069
- (cursor : Cursor , reader : JsonReader ) =>
1070
- alt.inject(cursor.decode(codec, reader))
1071
- }
1072
-
1073
- alternatives.foreach(alt => put(jsonLabel(alt), handler(alt)))
1074
- }
1075
-
1061
+ new TaggedUnionJCodec [U ](alternatives)(dispatch) {
1076
1062
def decodeValue (cursor : Cursor , in : JsonReader ): U = {
1077
1063
var result : U = null .asInstanceOf [U ]
1078
1064
if (in.isNextToken('{' )) {
1079
1065
if (! in.isNextToken('}' )) {
1080
1066
in.rollbackToken()
1081
1067
while ({
1082
1068
val key = in.readKeyAsString()
1069
+ cursor.push(key)
1083
1070
val handler = handlerMap.get(key)
1084
1071
if (handler eq null ) in.skip()
1085
1072
else if (in.isNextToken('n' )) {
@@ -1103,31 +1090,7 @@ private[smithy4s] class SchemaVisitorJCodec(
1103
1090
}
1104
1091
} else in.decodeError(" Expected JSON object" )
1105
1092
}
1106
- val precompiler = new smithy4s.schema.Alt .Precompiler [Writer ] {
1107
- def apply [A ](label : String , instance : Schema [A ]): Writer [A ] = {
1108
- val jsonLabel =
1109
- instance.hints.get(JsonName ).map(_.value).getOrElse(label)
1110
- val jcodecA = instance.compile(self)
1111
- a =>
1112
- out => {
1113
- out.writeObjectStart()
1114
- out.writeKey(jsonLabel)
1115
- jcodecA.encodeValue(a, out)
1116
- out.writeObjectEnd()
1117
- }
1118
- }
1119
- }
1120
- val writer = dispatch.compile(precompiler)
1121
-
1122
- def encodeValue (u : U , out : JsonWriter ): Unit = {
1123
- writer(u)(out)
1124
- }
1125
-
1126
- def decodeKey (in : JsonReader ): U =
1127
- in.decodeError(" Cannot use coproducts as keys" )
1128
1093
1129
- def encodeKey (u : U , out : JsonWriter ): Unit =
1130
- out.encodeError(" Cannot use coproducts as keys" )
1131
1094
}
1132
1095
1133
1096
private def untaggedUnion [U ](
0 commit comments