Skip to content

Commit 337b3bf

Browse files
authored
added json schema (#88)
* added json schema * update readme * updated version number
1 parent c5aca0a commit 337b3bf

File tree

7 files changed

+154
-26
lines changed

7 files changed

+154
-26
lines changed

README.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@ https://github.com/ess-dmsc/streaming-data-types
66

77
## FlatBuffer Schemas
88

9-
|name|description|
10-
|----|-----------|
11-
|hs00|Histogram schema (deprecated in favour of hs01)|
12-
|hs01|Histogram schema|
13-
|ns10|NICOS cache entry schema|
14-
|pl72|Run start|
15-
|6s4t|Run stop|
16-
|f142|Log data (deprecated in favour of f144)|
17-
|f144|Log data|
18-
|ev42|Event data (deprecated in favour of ev44)|
19-
|ev43|Event data from multiple pulses|
20-
|ev44|Event data with signed data types|
21-
|x5f2|Status messages|
22-
|tdct|Timestamps|
23-
|ep00|EPICS connection info (deprecated in favour of ep01)|
24-
|ep01|EPICS connection info|
25-
|rf5k|Forwarder configuration update|
26-
|answ|File-writer command response|
27-
|wrdn|File-writer finished writing|
28-
|NDAr|**Deprecated**|
29-
|ADAr|EPICS areaDetector data|
30-
|al00|Alarm/status messages used by the Forwarder and NICOS|
31-
|senv|**Deprecated**|
32-
|se00|Arrays with optional timestamps, for example waveform data. Replaces _senv_. |
9+
| name | description |
10+
|------|------------------------------------------------------------------------------|
11+
| hs00 | Histogram schema (deprecated in favour of hs01) |
12+
| hs01 | Histogram schema |
13+
| ns10 | NICOS cache entry schema |
14+
| pl72 | Run start |
15+
| 6s4t | Run stop |
16+
| f142 | Log data (deprecated in favour of f144) |
17+
| f144 | Log data |
18+
| ev42 | Event data (deprecated in favour of ev44) |
19+
| ev43 | Event data from multiple pulses |
20+
| ev44 | Event data with signed data types |
21+
| x5f2 | Status messages |
22+
| tdct | Timestamps |
23+
| ep00 | EPICS connection info (deprecated in favour of ep01) |
24+
| ep01 | EPICS connection info |
25+
| rf5k | Forwarder configuration update |
26+
| answ | File-writer command response |
27+
| wrdn | File-writer finished writing |
28+
| NDAr | **Deprecated** |
29+
| ADAr | EPICS areaDetector data |
30+
| al00 | Alarm/status messages used by the Forwarder and NICOS |
31+
| senv | **Deprecated** |
32+
| json | Generic JSON data |
33+
| se00 | Arrays with optional timestamps, for example waveform data. Replaces _senv_. |
3334

3435
### hs00 and hs01
3536
Schema for histogram data. It is one of the more complicated to use schemas.

README_DEV.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ This command can also be used to run the hooks manually.
2525
* Check whether the serialised data produced by the new code can be verified in C++
2626
* There is a helper program in the [FlatBufferVerification](https://github.com/ess-dmsc/FlatBufferVerification) repository
2727
* Don't worry if it fails verification - it seems to be an inherent FlatBuffers issue
28-
* Add the schema and verifiability result to the table of schemas in `README.md`
2928

3029
### Tox
3130
Tox allows the unit tests to be run against multiple versions of Python.

streaming_data_types/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
from streaming_data_types.histogram_hs00 import deserialise_hs00, serialise_hs00
2121
from streaming_data_types.histogram_hs01 import deserialise_hs01, serialise_hs01
22+
from streaming_data_types.json_json import deserialise_json, serialise_json
2223
from streaming_data_types.logdata_f142 import deserialise_f142, serialise_f142
2324
from streaming_data_types.logdata_f144 import deserialise_f144, serialise_f144
2425
from streaming_data_types.nicos_cache_ns10 import deserialise_ns10, serialise_ns10
@@ -56,6 +57,7 @@
5657
"se00": serialise_se00,
5758
"ADAr": serialise_ADAr,
5859
"al00": serialise_al00,
60+
"json": serialise_json,
5961
}
6062

6163

@@ -82,4 +84,5 @@
8284
"se00": deserialise_se00,
8385
"ADAr": deserialise_ADAr,
8486
"al00": deserialise_al00,
87+
"json": deserialise_json,
8588
}

streaming_data_types/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Version is not directly defined in __init__ because that causes all
22
# run time dependencies to become build-time dependencies when it is
33
# imported in setup.py
4-
version = "0.21.1"
4+
version = "0.22.0"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# automatically generated by the FlatBuffers compiler, do not modify
2+
3+
# namespace:
4+
5+
import flatbuffers
6+
from flatbuffers.compat import import_numpy
7+
8+
np = import_numpy()
9+
10+
11+
class JsonData(object):
12+
__slots__ = ["_tab"]
13+
14+
@classmethod
15+
def GetRootAs(cls, buf, offset=0):
16+
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
17+
x = JsonData()
18+
x.Init(buf, n + offset)
19+
return x
20+
21+
@classmethod
22+
def GetRootAsJsonData(cls, buf, offset=0):
23+
"""This method is deprecated. Please switch to GetRootAs."""
24+
return cls.GetRootAs(buf, offset)
25+
26+
@classmethod
27+
def JsonDataBufferHasIdentifier(cls, buf, offset, size_prefixed=False):
28+
return flatbuffers.util.BufferHasIdentifier(
29+
buf, offset, b"\x6A\x73\x6F\x6E", size_prefixed=size_prefixed
30+
)
31+
32+
# JsonData
33+
def Init(self, buf, pos):
34+
self._tab = flatbuffers.table.Table(buf, pos)
35+
36+
# JsonData
37+
def Json(self):
38+
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
39+
if o != 0:
40+
return self._tab.String(o + self._tab.Pos)
41+
return None
42+
43+
44+
def JsonDataStart(builder):
45+
builder.StartObject(1)
46+
47+
48+
def Start(builder):
49+
JsonDataStart(builder)
50+
51+
52+
def JsonDataAddJson(builder, json):
53+
builder.PrependUOffsetTRelativeSlot(
54+
0, flatbuffers.number_types.UOffsetTFlags.py_type(json), 0
55+
)
56+
57+
58+
def AddJson(builder: flatbuffers.Builder, json: int):
59+
JsonDataAddJson(builder, json)
60+
61+
62+
def JsonDataEnd(builder):
63+
return builder.EndObject()
64+
65+
66+
def End(builder):
67+
return JsonDataEnd(builder)

streaming_data_types/json_json.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import flatbuffers
2+
3+
import streaming_data_types.fbschemas.json_json.JsonData as JsonData
4+
from streaming_data_types.utils import check_schema_identifier
5+
6+
FILE_IDENTIFIER = b"json"
7+
8+
9+
def deserialise_json(buffer) -> str:
10+
check_schema_identifier(buffer, FILE_IDENTIFIER)
11+
return JsonData.JsonData.GetRootAsJsonData(buffer, 0).Json().decode("utf-8")
12+
13+
14+
def serialise_json(json_str) -> bytes:
15+
builder = flatbuffers.Builder(128)
16+
17+
offset = builder.CreateString(json_str)
18+
19+
JsonData.JsonDataStart(builder)
20+
JsonData.AddJson(builder, offset)
21+
result = JsonData.JsonDataEnd(builder)
22+
23+
builder.Finish(result, file_identifier=FILE_IDENTIFIER)
24+
return bytes(builder.Output())

tests/test_json.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import json
2+
3+
import pytest
4+
5+
from streaming_data_types import DESERIALISERS, SERIALISERS
6+
from streaming_data_types.exceptions import WrongSchemaException
7+
from streaming_data_types.json_json import deserialise_json, serialise_json
8+
9+
10+
class TestSerialisationJson:
11+
def test_serialises_and_deserialises_json_message_correctly(self):
12+
"""
13+
Round-trip to check what we serialise is what we get back.
14+
"""
15+
json_str = json.dumps(["foo", "bar"])
16+
buf = serialise_json(json_str)
17+
entry = deserialise_json(buf)
18+
19+
assert entry == json_str
20+
21+
def test_if_buffer_has_wrong_id_then_throws(self):
22+
json_str = json.dumps(["foo", "bar"])
23+
buf = serialise_json(json_str)
24+
25+
# Manually hack the id
26+
buf = bytearray(buf)
27+
buf[4:8] = b"1234"
28+
29+
with pytest.raises(WrongSchemaException):
30+
deserialise_json(buf)
31+
32+
def test_schema_type_is_in_global_serialisers_list(self):
33+
assert "json" in SERIALISERS
34+
assert "json" in DESERIALISERS

0 commit comments

Comments
 (0)