-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from OpenBankingToolkit/issue/871-fix-birthda…
…te-json-formatting 871: Adding custom Jackson serializer & deserializer for LocalDate objects
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...ng-model/src/main/java/com/forgerock/openbanking/serialiser/IsoLocalDateDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2019 ForgeRock AS. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.forgerock.openbanking.serialiser; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import org.joda.time.LocalDate; | ||
import org.joda.time.format.ISODateTimeFormat; | ||
|
||
import java.io.IOException; | ||
|
||
public class IsoLocalDateDeserializer extends StdDeserializer<LocalDate> { | ||
public IsoLocalDateDeserializer() { | ||
super(LocalDate.class); | ||
} | ||
|
||
@Override | ||
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { | ||
return ISODateTimeFormat.dateParser().parseLocalDate(jsonParser.getText()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...king-model/src/main/java/com/forgerock/openbanking/serialiser/IsoLocalDateSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Copyright 2019 ForgeRock AS. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.forgerock.openbanking.serialiser; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import org.joda.time.LocalDate; | ||
import org.joda.time.format.ISODateTimeFormat; | ||
|
||
import java.io.IOException; | ||
|
||
public class IsoLocalDateSerializer extends StdSerializer<LocalDate> { | ||
public IsoLocalDateSerializer() { | ||
super(LocalDate.class); | ||
} | ||
|
||
@Override | ||
public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | ||
jsonGenerator.writeObject(ISODateTimeFormat.date().print(localDate)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...odel/src/test/java/com/forgerock/openbanking/serialiser/IsoLocalDateDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2019 ForgeRock AS. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.forgerock.openbanking.serialiser; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import org.joda.time.LocalDate; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.BDDMockito; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class IsoLocalDateDeserializerTest { | ||
@Mock | ||
private JsonParser jsonParser; | ||
@Mock | ||
private DeserializationContext deserializationContext; | ||
|
||
@Test | ||
public void testDeserialize() throws IOException { | ||
BDDMockito.given(jsonParser.getText()).willReturn("2022-06-10"); | ||
final IsoLocalDateDeserializer deserializer = new IsoLocalDateDeserializer(); | ||
|
||
LocalDate date = deserializer.deserialize(jsonParser, deserializationContext); | ||
assertEquals(new LocalDate(2022, 6, 10), date); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...-model/src/test/java/com/forgerock/openbanking/serialiser/IsoLocalDateSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright 2019 ForgeRock AS. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package com.forgerock.openbanking.serialiser; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import org.joda.time.LocalDate; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.io.IOException; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class IsoLocalDateSerializerTest { | ||
@Mock | ||
private JsonGenerator jsonGenerator; | ||
@Mock | ||
private SerializerProvider serializerProvider; | ||
|
||
@Test | ||
public void testSerialize() throws IOException { | ||
final IsoLocalDateSerializer serializer = new IsoLocalDateSerializer(); | ||
final LocalDate date = new LocalDate(2022, 6, 10); | ||
|
||
serializer.serialize(date, jsonGenerator, serializerProvider); | ||
|
||
Mockito.verify(jsonGenerator).writeObject("2022-06-10"); | ||
} | ||
} |