Nyx Node
Loading...
Searching...
No Matches
json_string.py
1# -*- coding: utf-8 -*-
2########################################################################################################################
3# NyxNode
4# Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
5# SPDX-License-Identifier: GPL-3.0+
6########################################################################################################################
7
8import ctypes
9
10########################################################################################################################
11
12from .. import bind
13from .. import obj
14
15########################################################################################################################
16
18 """!
19 @brief JSON string object.
20 """
21
22 ####################################################################################################################
23
24 def __init__(self, value: str | bytes | None = None, ptr: int | None = None):
25 """!
26 @brief Allocates a new JSON string object or wraps one.
27
28 @param value Optional default value.
29 @param ptr Optional JSON string object pointer.
30 """
31
32 ################################################################################################################
33
34 if ptr is None:
35
36 ptr = bind.lib.nyx_string_new()
37
38 elif bind.lib.nyx_object_get_type(ptr) != bind.NyxObjectType.STRING:
39
40 raise TypeError('Not a pointer to a Nyx string object')
41
42 ################################################################################################################
43
44 super().__init__(ptr)
45
46 ################################################################################################################
47
48 if isinstance(value, str):
49
50 self.value = value
51
52 if isinstance(value, bytes):
53
54 self.buff = value
55
56 ####################################################################################################################
57
58 def _get_buff(self) -> bytes:
59
60 ################################################################################################################
61
62 result_size = bind.c_size_t()
63 result_buff = bind.c_void_p()
64
65 bind.lib.nyx_string_get_buff(self.ptr, ctypes.byref(result_size), ctypes.byref(result_buff))
66
67 ################################################################################################################
68
69 return ctypes.string_at(result_buff.value, result_size.value)
70
71 ####################################################################################################################
72
73 @property
74 def value(self) -> str:
75 """!
76 @brief Gets the text value of this JSON string object.
77
78 @return The current text value.
79
80 @note The returned text value remains valid until the object is modified or released.
81 """
82
83 return self._get_buff().decode('utf-8')
84
85 ####################################################################################################################
86
87 @property
88 def buff(self) -> bytes:
89 """!
90 @brief Gets the content of this JSON string object as a byte buffer.
91
92 @return The current content bytes.
93
94 @note The returned buffer remains valid until the object is modified or released.
95 """
96
97 return self._get_buff()#decode('utf-8')
98
99 ####################################################################################################################
100
101 @value.setter
102 def value(self, value: str) -> bool:
103 """!
104 @brief Sets the text value of this JSON string object.
105
106 @return @c True if the value was modified, @c False otherwise.
107 """
108
109 ################################################################################################################
110
111 data = bind.as_bytes(value, allow_none = False)
112
113 buff = ctypes.cast(bind.lib.nyx_string_ndup(data, len(data)), bind.c_char_p)
114
115 ################################################################################################################
116
117 return bool(bind.lib.nyx_string_set(
118 self.ptr,
119 buff,
120 True
121 ))
122
123 ####################################################################################################################
124
125 @buff.setter
126 def buff(self, value: bytes) -> bool:
127 """!
128 @brief Sets the content of this JSON string object as a byte buffer.
129
130 @return @c True if the value was modified, @c False otherwise.
131 """
132
133 ################################################################################################################
134
135 data = bind.as_bytes(value, allow_none = False)
136
137 buff = ctypes.cast(bind.lib.nyx_buffer_ndup(data, len(data)), bind.c_void_p)
138
139 ################################################################################################################
140
141 return bool(bind.lib.nyx_string_set_buff(
142 self.ptr,
143 len(data),
144 buff,
145 True
146 ))
147
148########################################################################################################################
149
150__all__ = ['NyxString']
151
152########################################################################################################################
__init__(self, str|bytes|None value=None, int|None ptr=None)
Allocates a new JSON string object or wraps one.
Base class for JSON Nyx objects.
Definition obj.py:44