Search in sources :

Example 6 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class JsonSerializerTestBase method encodesJsonArray.

@Test
void encodesJsonArray() {
    JsonArray input = JsonArray.from("1", true, 2);
    byte[] output = serializer().serialize(input);
    assertEquals("[\"1\",true,2]", new String(output, UTF_8));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Test(org.junit.jupiter.api.Test)

Example 7 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class JsonSerializerTestBase method decodesJsonArray.

@Test
void decodesJsonArray() {
    byte[] input = "[\"1\",true,2]".getBytes(UTF_8);
    JsonArray decoded = serializer().deserialize(JsonArray.class, input);
    assertEquals(JsonArray.fromJson(input), decoded);
    JsonArray decodedWithTypeRef = serializer().deserialize(new TypeRef<JsonArray>() {
    }, input);
    assertEquals(decoded, decodedWithTypeRef);
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Test(org.junit.jupiter.api.Test)

Example 8 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class JsonTranscoderTest method encodesJsonArray.

@Test
void encodesJsonArray() {
    JsonArray input = JsonArray.from("1", true, 2);
    Transcoder.EncodedValue output = JSON_TRANSCODER.encode(input);
    assertEquals("[\"1\",true,2]", new String(output.encoded(), StandardCharsets.UTF_8));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Test(org.junit.jupiter.api.Test)

Example 9 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class ViewOptionsTest method shouldStoreKeysAsJsonOutsideParams.

@Test
void shouldStoreKeysAsJsonOutsideParams() {
    JsonArray keys = JsonArray.create().add("1").add("2").add("3");
    ViewOptions options = viewOptions();
    assertNull(options.build().keys());
    options.keys(keys);
    assertEquals(JsonObject.create().put("keys", keys).toString(), options.build().keys());
    assertFalse(options.export().contains("keys="));
    assertFalse(options.export().contains("3"));
}
Also used : JsonArray(com.couchbase.client.java.json.JsonArray) Test(org.junit.jupiter.api.Test)

Example 10 with JsonArray

use of com.couchbase.client.java.json.JsonArray in project couchbase-jvm-clients by couchbase.

the class SearchRow method fromResponse.

public static SearchRow fromResponse(final SearchChunkRow row, final JsonSerializer serializer) {
    try {
        JsonObject hit = JacksonTransformers.MAPPER.readValue(row.data(), JsonObject.class);
        String index = hit.getString("index");
        String id = hit.getString("id");
        double score = hit.getDouble("score");
        JsonObject explanationJson = defaultIfNull(hit.getObject("explanation"), JsonObject::create);
        Optional<SearchRowLocations> locations = Optional.ofNullable(hit.getObject("locations")).map(SearchRowLocations::from);
        JsonObject fragmentsJson = hit.getObject("fragments");
        Map<String, List<String>> fragments;
        if (fragmentsJson != null) {
            fragments = new HashMap<>(fragmentsJson.size());
            for (String field : fragmentsJson.getNames()) {
                List<String> fragment;
                JsonArray fragmentJson = fragmentsJson.getArray(field);
                if (fragmentJson != null) {
                    fragment = new ArrayList<>(fragmentJson.size());
                    for (int i = 0; i < fragmentJson.size(); i++) {
                        fragment.add(fragmentJson.getString(i));
                    }
                } else {
                    fragment = Collections.emptyList();
                }
                fragments.put(field, fragment);
            }
        } else {
            fragments = Collections.emptyMap();
        }
        byte[] fields = null;
        if (hit.containsKey("fields")) {
            // daschl: this is a bit wasteful and should be streamlined
            fields = JacksonTransformers.MAPPER.writeValueAsBytes(hit.getObject("fields").toMap());
        }
        return new SearchRow(index, id, score, explanationJson, locations, fragments, fields, serializer);
    } catch (IOException e) {
        throw new DecodingFailureException("Failed to decode row '" + new String(row.data(), UTF_8) + "'", e);
    }
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) IOException(java.io.IOException) JsonArray(com.couchbase.client.java.json.JsonArray) ArrayList(java.util.ArrayList) List(java.util.List) DecodingFailureException(com.couchbase.client.core.error.DecodingFailureException)

Aggregations

JsonArray (com.couchbase.client.java.json.JsonArray)48 Test (org.junit.jupiter.api.Test)26 JsonObject (com.couchbase.client.java.json.JsonObject)24 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)3 List (java.util.List)3 Airport (org.springframework.data.couchbase.domain.Airport)3 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)2 InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)2 RetryExhaustedException (com.couchbase.client.core.retry.reactor.RetryExhaustedException)2 JsonValue (com.couchbase.client.java.json.JsonValue)2 GetResult (com.couchbase.client.java.kv.GetResult)2 QueryOptions (com.couchbase.client.java.query.QueryOptions)2 SearchQuery (com.couchbase.client.java.search.SearchQuery)2 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Query (org.springframework.data.couchbase.core.query.Query)2 AirportRepositoryScanConsistencyTest (org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest)2