-
Notifications
You must be signed in to change notification settings - Fork 215
/
DittoBsonJson.java
150 lines (130 loc) · 5.82 KB
/
DittoBsonJson.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.persistence.mongo;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonValue;
/**
* This is a specialized MongoDB BSON converter which additionally takes care that in JSON keys dots "." and dollar
* signs "$" are replaced with their unicode representations in the {@link #parse(JsonObject)} {@link #parse(JsonArray)}
* function and vice versa in the {@link #serialize(BsonValue)} function.
*/
public final class DittoBsonJson {
private static final DittoBsonJson INSTANCE = DittoBsonJson.newInstance();
private final JsonValueToDbEntityMapper jsonValueToDbEntityMapper;
private final Function<BsonDocument, JsonObject> bsonDocumentToJsonObjectMapper;
private final Function<BsonArray, JsonArray> bsonArrayToJsonObjectMapper;
/*
* Inhibit instantiation of this utility class.
*/
private DittoBsonJson(final JsonValueToDbEntityMapper jsonValueToDbEntityMapper,
final Function<BsonDocument, JsonObject> bsonDocumentToJsonObjectMapper,
final Function<BsonArray, JsonArray> bsonArrayToJsonObjectMapper) {
this.jsonValueToDbEntityMapper = jsonValueToDbEntityMapper;
this.bsonDocumentToJsonObjectMapper = bsonDocumentToJsonObjectMapper;
this.bsonArrayToJsonObjectMapper = bsonArrayToJsonObjectMapper;
}
/**
* Returns an instance of {@code DittoBsonJSON}.
*
* @return the instance.
*/
public static DittoBsonJson getInstance() {
return INSTANCE;
}
private static DittoBsonJson newInstance() {
final KeyNameReviser jsonToMongoDbKeyNameReviser = KeyNameReviser.escapeProblematicPlainChars();
final KeyNameReviser jsonKeyNameReviser = KeyNameReviser.decodeKnownUnicodeChars();
return new DittoBsonJson(JsonValueToDbEntityMapper.of(jsonToMongoDbKeyNameReviser),
BsonDocumentToJsonObjectMapper.getInstance(jsonKeyNameReviser),
BsonArrayToJsonObjectMapper.getInstance(jsonKeyNameReviser));
}
/**
* Serializes the specified {@link BsonValue} to Json, applying replacement of "special" characters {@code "$"} and
* {@code "."}.
*
* @param bsonValue the BsonValue to be serialized.
* @return the BsonValue serialized as JsonValue.
* @throws NullPointerException if {@code bsonValue} is {@code null}.
* @throws IllegalArgumentException if {@code bsonValue} is not an instance of {@link BsonDocument} or {@link
* BsonArray}.
*/
public JsonValue serialize(final BsonValue bsonValue) {
checkNotNull(bsonValue, "BsonValue to be serialized");
if (bsonValue instanceof BsonDocument) {
return serialize((BsonDocument) bsonValue);
} else if (bsonValue instanceof BsonArray) {
return serialize((BsonArray) bsonValue);
} else {
throw new IllegalArgumentException("Can only serialize BsonDocument or BsonArray");
}
}
/**
* Serializes the specified {@link BsonDocument} to Json, applying replacement of "special" characters {@code "$"}
* and {@code "."}.
*
* @param bsonDocument the BsonDocument to be serialized.
* @return the BsonDocument serialized as JsonValue.
* @throws NullPointerException if {@code bsonDocument} is {@code null}.
*/
public JsonObject serialize(final BsonDocument bsonDocument) {
return bsonDocumentToJsonObjectMapper.apply(checkNotNull(bsonDocument, "BsonDocument to be serialized"));
}
/**
* Serializes the specified {@link BsonArray} to Json, applying replacement of "special" characters {@code "$"} and
* {@code "."}.
*
* @param bsonArray the BsonArray to be serialized.
* @return the BsonArray serialized as JsonValue.
* @throws NullPointerException if {@code bsonArray} is {@code null}.
*/
public JsonArray serialize(final BsonArray bsonArray) {
return bsonArrayToJsonObjectMapper.apply(checkNotNull(bsonArray, "BsonArray to be serialized"));
}
/**
* Parses the specified {@link JsonObject} into an {@link BsonDocument}.
*
* @param jsonObject the JSON object to be parsed.
* @return the parsed JSON object as BsonDocument.
* @throws NullPointerException if {@code jsonObject} is {@code null}.
*/
public BsonDocument parse(final JsonObject jsonObject) {
return jsonValueToDbEntityMapper.mapJsonObjectToBsonDocument(jsonObject);
}
/**
* Parses the passed in {@link JsonArray} into an {@link BsonArray}.
*
* @param jsonArray the JSON array to be parsed.
* @return the parsed JSON array as BsonArray.
* @throws NullPointerException if {@code jsonArray} is {@code null}.
*/
public BsonArray parse(final JsonArray jsonArray) {
return jsonValueToDbEntityMapper.mapJsonArrayToBsonArray(jsonArray);
}
/**
* Parses the passed in {@link JsonValue} into an {@link BsonValue}.
*
* @param jsonValue the JSON value to be parsed.
* @return the parsed JSON value as BsonValue.
*/
@Nullable
public BsonValue parseValue(final JsonValue jsonValue) {
return jsonValueToDbEntityMapper.mapJsonValueToBsonValue(jsonValue);
}
}