Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#180] Add a JsonCodec #191

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/io/r2dbc/h2/codecs/DefaultCodecs.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static List<Codec<?>> createCodecs(Client client, ClassLoader classLoader, Codec
new DoubleCodec(),
new FloatCodec(),
new IntegerCodec(),
new JsonCodec(),
new LocalDateCodec(),
new LocalDateTimeCodec(client),
new LocalTimeCodec(),
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/io/r2dbc/h2/codecs/JsonCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed 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
*
* https://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 io.r2dbc.h2.codecs;

import io.r2dbc.h2.util.Assert;
import org.h2.value.Value;
import org.h2.value.ValueJson;

final class JsonCodec extends AbstractCodec<String> {

JsonCodec() {
super(String.class);
}

@Override
boolean doCanDecode(int dataType) {
return Value.JSON == dataType;
}

@Override
String doDecode(Value value, Class<? extends String> type) {
return value.getString();
}

@Override
Value doEncode(String value) {
return ValueJson.fromJson(Assert.requireNonNull(value, "value must not be null"));
}
}
2 changes: 2 additions & 0 deletions src/test/java/io/r2dbc/h2/codecs/DefaultCodecsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void createCodecsWithNonOptionalCodecsAndNoDuplicates() throws Exception {
DoubleCodec.class,
FloatCodec.class,
IntegerCodec.class,
JsonCodec.class,
LocalDateCodec.class,
LocalDateTimeCodec.class,
LocalTimeCodec.class,
Expand Down Expand Up @@ -116,6 +117,7 @@ void createCodecsWithOptionalCodecsAndNoDuplicates() throws Exception {
FloatCodec.class,
GeometryCodec.class,
IntegerCodec.class,
JsonCodec.class,
LocalDateCodec.class,
LocalDateTimeCodec.class,
LocalTimeCodec.class,
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/io/r2dbc/h2/codecs/JsonCodecTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed 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
*
* https://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 io.r2dbc.h2.codecs;

import org.h2.value.Value;
import org.h2.value.ValueInt;
import org.h2.value.ValueJson;
import org.h2.value.ValueNull;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

final class JsonCodecTest {

@Test
void decode() {
assertThat(new JsonCodec().decode(ValueJson.fromJson("{}"), String.class))
.isEqualTo("{}");
}

@Test
void doCanDecode() {
JsonCodec codec = new JsonCodec();

assertThat(codec.doCanDecode(Value.JSON)).isTrue();
assertThat(codec.doCanDecode(Value.DOUBLE)).isFalse();
}

@Test
void doEncode() {
assertThat(new JsonCodec().doEncode("{}"))
.isEqualTo(ValueJson.fromJson("{}"));
}

@Test
void doEncodeNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new JsonCodec().doEncode(null))
.withMessage("value must not be null");
}

@Test
void encodeNull() {
assertThat(new JsonCodec().encodeNull())
.isEqualTo(ValueNull.INSTANCE);
}
}