-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDKQE-3406 :- Add custom serializer in seach option
1. Add pass serializer search option is custom serializer is set to true 2. Implement a custom serializer 3. check if fields in result are null, then don't parse then according to the fieldAs input Change-Id: I498d9d3316a3587e2e925206a931f283fe814c43 Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/214786 Reviewed-by: Graham Pople <graham.pople@couchbase.com> Tested-by: Build Bot <build@couchbase.com>
- Loading branch information
1 parent
b825e4a
commit 9771679
Showing
3 changed files
with
114 additions
and
15 deletions.
There are no files selected for viewing
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
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
57 changes: 57 additions & 0 deletions
57
java-fit-performer/src/main/java/com/couchbase/utils/CustomJsonSerializer.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,57 @@ | ||
/** | ||
* CustomJsonSerializer provides a generic implementation of the JsonSerializer interface. | ||
* This serializer is designed to handle the conversion of Java objects to JSON format | ||
* and back, with an additional boolean flag (`Serialized`) that indicates whether | ||
* the object has been serialized. The flag is included in the JSON payload, making | ||
* it easy to track the serialization state of objects. | ||
* Use Cases: | ||
* - This serializer can be used in scenarios where you need to serialize and deserialize | ||
* objects while keeping track of their serialization state. | ||
* Limitations: | ||
* - The current implementation assumes that the input objects can be serialized into | ||
* a JSON format using Jackson's ObjectMapper. Complex or non-standard objects may | ||
* require additional handling. | ||
* - The `deserialize` methods in this implementation modify the original JSON object | ||
* by setting the `Serialized` flag to `false`, which might not be suitable for | ||
* all use cases. | ||
*/ | ||
|
||
package com.couchbase.utils; | ||
|
||
import com.couchbase.client.core.deps.com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.couchbase.client.core.error.DecodingFailureException; | ||
import com.couchbase.client.core.json.Mapper; | ||
import com.couchbase.client.java.codec.JsonSerializer; | ||
import com.couchbase.client.java.codec.TypeRef; | ||
import com.couchbase.client.java.json.JsonObject; | ||
|
||
|
||
public class CustomJsonSerializer implements JsonSerializer { | ||
@Override | ||
public byte[] serialize(Object input) { | ||
try { | ||
String json = Mapper.writer().writeValueAsString(input); | ||
var obj = JsonObject.create().put("Serialized", true); | ||
return obj.toBytes(); | ||
} catch (JsonProcessingException e) { | ||
throw new DecodingFailureException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(Class<T> target, byte[] input) { | ||
JsonObject obj = JsonObject.fromJson(input); | ||
obj.put("Serialized", false); | ||
return (T) obj; | ||
} | ||
|
||
@Override | ||
public <T> T deserialize(TypeRef<T> target, byte[] input) { | ||
JsonObject obj = JsonObject.fromJson(input); | ||
obj.put("Serialized", false); | ||
return (T) obj; | ||
} | ||
} |