-
Notifications
You must be signed in to change notification settings - Fork 12
/
FieldClass.java
94 lines (84 loc) · 3.29 KB
/
FieldClass.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
/*******************************************************************************
* Copyright (c) 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Patrick Tasse - Initial implementation
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata;
import java.lang.reflect.Type;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
/**
* Class that stores a field class property, which can be a JSON object or a
* string referring to a field class alias.
*/
public class FieldClass {
private final JsonObject fFieldClass;
/**
* Field class deserializer
*/
public static class FieldClassDeserializer implements JsonDeserializer<FieldClass> {
private final ICTFMetadataNode fRoot;
/**
* Constructor
*
* @param root
* the CTF metadata root node
*/
public FieldClassDeserializer(ICTFMetadataNode root) {
fRoot = root;
}
@Override
public FieldClass deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (jElement.isJsonPrimitive()) {
JsonPrimitive jPrimitive = jElement.getAsJsonPrimitive();
if (jPrimitive.isString()) {
String fieldClassAlias = jPrimitive.getAsString();
for (ICTFMetadataNode node : fRoot.getChildren()) {
if (node instanceof JsonFieldClassAliasMetadataNode) {
JsonFieldClassAliasMetadataNode fieldClassAliasNode = (JsonFieldClassAliasMetadataNode) node;
if (fieldClassAliasNode.getName().equals(fieldClassAlias)) {
return new FieldClass(fieldClassAliasNode.getFieldClass());
}
}
}
throw new JsonParseException("no previously occurring field class alias named '" + fieldClassAlias + '\''); //$NON-NLS-1$
}
} else if (jElement.isJsonObject()) {
JsonObject jObject = jElement.getAsJsonObject();
return new FieldClass(jObject);
}
throw new JsonParseException("field-class property is not a JSON object or JSON string"); //$NON-NLS-1$
}
}
/**
* Constructor
*
* @param fieldClass
* the field class JSON object
*/
public FieldClass(JsonObject fieldClass) {
fFieldClass = fieldClass;
}
/**
* Get the field class as a JSON object
*
* @return the field class
*/
public JsonObject getFieldClass() {
return fFieldClass;
}
}