1+ #region License
2+
3+ /*
4+ * Licensed to the Apache Software Foundation (ASF) under one
5+ * or more contributor license agreements. See the NOTICE file
6+ * distributed with this work for additional information
7+ * regarding copyright ownership. The ASF licenses this file
8+ * to you under the Apache License, Version 2.0 (the
9+ * "License"); you may not use this file except in compliance
10+ * with the License. You may obtain a copy of the License at
11+ *
12+ * http://www.apache.org/licenses/LICENSE-2.0
13+ *
14+ * Unless required by applicable law or agreed to in writing,
15+ * software distributed under the License is distributed on an
16+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+ * KIND, either express or implied. See the License for the
18+ * specific language governing permissions and limitations
19+ * under the License.
20+ */
21+
22+ #endregion
23+
24+ using System ;
25+ using System . IO ;
26+ using System . Threading ;
27+ using System . Threading . Tasks ;
28+
29+ namespace Gremlin . Net . Structure . IO . GraphBinary4
30+ {
31+ /// <summary>
32+ /// Allows to serialize objects to GraphBinary v4.
33+ /// </summary>
34+ public class GraphBinaryWriter4
35+ {
36+ private const byte ValueFlagNull = 1 ;
37+ private const byte ValueFlagNone = 0 ;
38+
39+ /// <summary>
40+ /// A <see cref="byte"/> representing the version of the GraphBinary v4 specification.
41+ /// </summary>
42+ public const byte VersionByte = 0x84 ;
43+
44+ private static readonly byte [ ] UnspecifiedNullBytes = { DataType4 . UnspecifiedNull . TypeCode , 0x01 } ;
45+
46+ private readonly TypeSerializerRegistry4 _registry ;
47+
48+ /// <summary>
49+ /// Initializes a new instance of the <see cref="GraphBinaryWriter4" /> class.
50+ /// </summary>
51+ /// <param name="registry">The <see cref="TypeSerializerRegistry4"/> to use for serialization.</param>
52+ public GraphBinaryWriter4 ( TypeSerializerRegistry4 ? registry = null )
53+ {
54+ _registry = registry ?? TypeSerializerRegistry4 . Instance ;
55+ }
56+
57+ /// <summary>
58+ /// Writes a nullable value without including type information.
59+ /// </summary>
60+ /// <param name="value">The value to write.</param>
61+ /// <param name="stream">The stream to write to.</param>
62+ /// <param name="cancellationToken">The token to cancel the operation. The default value is None.</param>
63+ /// <returns>A task that represents the asynchronous write operation.</returns>
64+ public async Task WriteNullableValueAsync ( object ? value , Stream stream ,
65+ CancellationToken cancellationToken = default )
66+ {
67+ if ( value == null )
68+ {
69+ await WriteValueFlagNullAsync ( stream , cancellationToken ) . ConfigureAwait ( false ) ;
70+ return ;
71+ }
72+
73+ var valueType = value . GetType ( ) ;
74+ var serializer = _registry . GetSerializerFor ( valueType ) ;
75+ await serializer . WriteNullableValueAsync ( value , stream , this , cancellationToken ) . ConfigureAwait ( false ) ;
76+ }
77+
78+ /// <summary>
79+ /// Writes a non-nullable value without including type information.
80+ /// </summary>
81+ /// <param name="value">The value to write.</param>
82+ /// <param name="stream">The stream to write to.</param>
83+ /// <param name="cancellationToken">The token to cancel the operation. The default value is None.</param>
84+ /// <returns>A task that represents the asynchronous write operation.</returns>
85+ public async Task WriteNonNullableValueAsync ( object value , Stream stream ,
86+ CancellationToken cancellationToken = default )
87+ {
88+ if ( value == null ) throw new IOException ( $ "{ nameof ( value ) } cannot be null") ;
89+ var valueType = value . GetType ( ) ;
90+ var serializer = _registry . GetSerializerFor ( valueType ) ;
91+ await serializer . WriteNonNullableValueAsync ( value , stream , this , cancellationToken ) . ConfigureAwait ( false ) ;
92+ }
93+
94+ /// <summary>
95+ /// Writes an object in fully-qualified format, containing {type_code}{type_info}{value_flag}{value}.
96+ /// </summary>
97+ /// <param name="value">The value to write.</param>
98+ /// <param name="stream">The stream to write to.</param>
99+ /// <param name="cancellationToken">The token to cancel the operation. The default value is None.</param>
100+ /// <returns>A task that represents the asynchronous write operation.</returns>
101+ public async Task WriteAsync ( object ? value , Stream stream , CancellationToken cancellationToken = default )
102+ {
103+ if ( value == null )
104+ {
105+ await stream . WriteAsync ( UnspecifiedNullBytes , cancellationToken ) . ConfigureAwait ( false ) ;
106+ return ;
107+ }
108+
109+ var valueType = value . GetType ( ) ;
110+ var serializer = _registry . GetSerializerFor ( valueType ) ;
111+
112+ await stream . WriteByteAsync ( serializer . DataType . TypeCode , cancellationToken ) . ConfigureAwait ( false ) ;
113+ await serializer . WriteAsync ( value , stream , this , cancellationToken ) . ConfigureAwait ( false ) ;
114+ }
115+
116+ /// <summary>
117+ /// Writes a single byte representing the null value_flag.
118+ /// </summary>
119+ /// <param name="stream">The stream to write to.</param>
120+ /// <param name="cancellationToken">The token to cancel the operation. The default value is None.</param>
121+ /// <returns>A task that represents the asynchronous write operation.</returns>
122+ public async Task WriteValueFlagNullAsync ( Stream stream , CancellationToken cancellationToken = default )
123+ {
124+ await stream . WriteByteAsync ( ValueFlagNull , cancellationToken ) . ConfigureAwait ( false ) ;
125+ }
126+
127+ /// <summary>
128+ /// Writes a single byte with value 0, representing an unset value_flag.
129+ /// </summary>
130+ /// <param name="stream">The stream to write to.</param>
131+ /// <param name="cancellationToken">The token to cancel the operation. The default value is None.</param>
132+ /// <returns>A task that represents the asynchronous write operation.</returns>
133+ public async Task WriteValueFlagNoneAsync ( Stream stream , CancellationToken cancellationToken = default ) {
134+ await stream . WriteByteAsync ( ValueFlagNone , cancellationToken ) . ConfigureAwait ( false ) ;
135+ }
136+
137+
138+ }
139+ }
0 commit comments