-
Notifications
You must be signed in to change notification settings - Fork 22
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 #242 from EventStore/yoeight/db-428-improve-java-c…
…lient-stream-metadata-serialization Improve stream metadata serialization.
- Loading branch information
Showing
10 changed files
with
193 additions
and
234 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
87 changes: 87 additions & 0 deletions
87
db-client-java/src/main/java/com/eventstore/dbclient/CustomAclCodec.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,87 @@ | ||
package com.eventstore.dbclient; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public final class CustomAclCodec { | ||
public static class ListSerializer extends JsonSerializer<List<String>> { | ||
@Override | ||
public void serialize(List<String> value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
if (value.size() == 1) { | ||
gen.writeString(value.get(0)); | ||
} else { | ||
gen.writeStartArray(); | ||
|
||
for (String s: value) { | ||
gen.writeString(s); | ||
} | ||
|
||
gen.writeEndArray(); | ||
} | ||
} | ||
} | ||
|
||
public static class ListDeserializer extends JsonDeserializer<List<String>> { | ||
@Override | ||
public List<String> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
if (p.isExpectedStartArrayToken()) { | ||
List<String> list = new ArrayList<>(); | ||
|
||
while (!p.nextToken().isStructEnd()) { | ||
list.add(p.getValueAsString()); | ||
} | ||
|
||
return list; | ||
} | ||
|
||
return Collections.singletonList(p.getValueAsString()); | ||
} | ||
} | ||
|
||
public static class Serializer extends JsonSerializer<Acl> { | ||
@Override | ||
public void serialize(Acl value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
if (value instanceof SystemStreamAcl) { | ||
gen.writeString(SystemStreamAcl.ACL_NAME); | ||
return; | ||
} | ||
|
||
if (value instanceof UserStreamAcl) { | ||
gen.writeString(UserStreamAcl.ACL_NAME); | ||
return; | ||
} | ||
|
||
gen.writePOJO(value); | ||
} | ||
} | ||
|
||
public static class Deserializer extends JsonDeserializer<Acl> { | ||
@Override | ||
public Acl deserialize(JsonParser p, DeserializationContext ctx) throws IOException { | ||
if (p.currentToken() == JsonToken.VALUE_STRING) { | ||
String value = p.getText(); | ||
|
||
if (value.equals(UserStreamAcl.ACL_NAME)) | ||
return UserStreamAcl.getInstance(); | ||
|
||
if (value.equals(SystemStreamAcl.ACL_NAME)) { | ||
return SystemStreamAcl.getInstance(); | ||
} | ||
|
||
throw new IOException(String.format("Unknown ACL type '%s'", value)); | ||
} | ||
|
||
return p.readValueAs(StreamAcl.class); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.