33
44package com .azure .security .attestation .models ;
55
6- import com .fasterxml .jackson .annotation .JsonProperty ;
6+ import com .azure .json .JsonReader ;
7+ import com .azure .json .JsonSerializable ;
8+ import com .azure .json .JsonToken ;
9+ import com .azure .json .JsonWriter ;
710
11+ import java .io .IOException ;
812import java .time .Instant ;
913import java .time .OffsetDateTime ;
1014import java .time .ZoneOffset ;
15+ import java .util .List ;
1116
12- final class TestObject {
13- @ JsonProperty (value = "alg" )
17+ public final class TestObject implements JsonSerializable <TestObject > {
1418 private String alg ;
19+ private int integer ;
20+ private long expiration ;
21+ private long issuedOn ;
22+ private long notBefore ;
23+ private int [] integerArray ;
24+ String issuer ;
25+
26+ TestObject () {
27+ }
1528
1629 public TestObject setAlg (String v ) {
1730 alg = v ;
@@ -22,9 +35,6 @@ public String getAlg() {
2235 return alg ;
2336 }
2437
25- @ JsonProperty (value = "int" )
26- private int integer ;
27-
2838 public TestObject setInteger (int v ) {
2939 integer = v ;
3040 return this ;
@@ -34,9 +44,6 @@ public int getInteger() {
3444 return integer ;
3545 }
3646
37- @ JsonProperty (value = "exp" )
38- private long expiration ;
39-
4047 public TestObject setExpiresOn (OffsetDateTime expirationTime ) {
4148 this .expiration = expirationTime .toEpochSecond ();
4249 return this ;
@@ -46,9 +53,6 @@ public OffsetDateTime getExpiresOn() {
4653 return OffsetDateTime .ofInstant (Instant .EPOCH .plusSeconds (expiration ), ZoneOffset .UTC );
4754 }
4855
49- @ JsonProperty (value = "iat" )
50- private long issuedOn ;
51-
5256 public TestObject setIssuedOn (OffsetDateTime issuedOn ) {
5357 this .issuedOn = issuedOn .toEpochSecond ();
5458 return this ;
@@ -58,9 +62,6 @@ public OffsetDateTime getIssuedOn() {
5862 return OffsetDateTime .ofInstant (Instant .EPOCH .plusSeconds (issuedOn ), ZoneOffset .UTC );
5963 }
6064
61- @ JsonProperty (value = "nbf" )
62- private long notBefore ;
63-
6465 public TestObject setNotBefore (OffsetDateTime notBefore ) {
6566 this .notBefore = notBefore .toEpochSecond ();
6667 return this ;
@@ -70,9 +71,6 @@ public OffsetDateTime getNotBefore() {
7071 return OffsetDateTime .ofInstant (Instant .EPOCH .plusSeconds (notBefore ), ZoneOffset .UTC );
7172 }
7273
73- @ JsonProperty (value = "intArray" )
74- private int [] integerArray ;
75-
7674 public TestObject setIntegerArray (int [] v ) {
7775 integerArray = v .clone ();
7876 return this ;
@@ -82,9 +80,6 @@ public int[] getIntegerArray() {
8280 return integerArray ;
8381 }
8482
85- @ JsonProperty (value = "iss" )
86- String issuer ;
87-
8883 public TestObject setIssuer (String iss ) {
8984 issuer = iss ;
9085 return this ;
@@ -94,7 +89,63 @@ public String getIssuer() {
9489 return issuer ;
9590 }
9691
97- TestObject () {
92+ @ Override
93+ public JsonWriter toJson (JsonWriter jsonWriter ) throws IOException {
94+ jsonWriter .writeStartObject ()
95+ .writeStringField ("alg" , alg )
96+ .writeIntField ("int" , integer )
97+ .writeLongField ("exp" , expiration )
98+ .writeLongField ("iat" , issuedOn )
99+ .writeLongField ("nbf" , notBefore );
100+
101+ if (integerArray != null ) {
102+ jsonWriter .writeStartArray ("intArray" );
103+
104+ for (int value : integerArray ) {
105+ jsonWriter .writeInt (value );
106+ }
107+
108+ jsonWriter .writeEndArray ();
109+ }
110+
111+ return jsonWriter .writeStringField ("iss" , issuer ).writeEndObject ();
112+ }
98113
114+ public static TestObject fromJson (JsonReader jsonReader ) throws IOException {
115+ return jsonReader .readObject (reader -> {
116+ TestObject testObject = new TestObject ();
117+
118+ while (reader .nextToken () != JsonToken .END_OBJECT ) {
119+ String fieldName = reader .getFieldName ();
120+ reader .nextToken ();
121+
122+ if ("alg" .equals (fieldName )) {
123+ testObject .alg = reader .getString ();
124+ } else if ("int" .equals (fieldName )) {
125+ testObject .integer = reader .getInt ();
126+ } else if ("exp" .equals (fieldName )) {
127+ testObject .expiration = reader .getLong ();
128+ } else if ("iat" .equals (fieldName )) {
129+ testObject .issuedOn = reader .getLong ();
130+ } else if ("nbf" .equals (fieldName )) {
131+ testObject .notBefore = reader .getLong ();
132+ } else if ("intArray" .equals (fieldName )) {
133+ List <Integer > intList = reader .readArray (JsonReader ::getInt );
134+ if (intList != null ) {
135+ testObject .integerArray = new int [intList .size ()];
136+ for (int i = 0 ; i < testObject .integerArray .length ; i ++) {
137+ testObject .integerArray [i ] = intList .get (i );
138+ }
139+
140+ }
141+ } else if ("iss" .equals (fieldName )) {
142+ testObject .issuer = reader .getString ();
143+ } else {
144+ reader .skipChildren ();
145+ }
146+ }
147+
148+ return testObject ;
149+ });
99150 }
100151}
0 commit comments