Enhanced C#
Loyc library documentation
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
Properties | List of all members
SyncJson.Options.ForReader Class Reference

Source file:

Properties

bool AllowComments [get, set]
 Whether to accept // and /* */ comments when reading JSON. More...
 
bool Strict = true [get, set]
 Whether to follow JSON rules strictly when reading JSON. When this mode is disabled, the following syntax does not cause an exception: (1) a comma before a closing ']' or '}', (2) a number with leading '.' or leading '0' (with other digits), (3) the \0 (null character) escape sequence, (4) invalid escape sequences (instead, is read as if it were \q), (5) non-string object keys (which are essentially ignored). More...
 
int MaxDepth = false [get, set]
 If objects are nested more than this many levels deep, an exception will be thrown and the reader will refuse to read further. This option helps avoid StackOverflowException while reading, which normally forces a .NET process to terminate. More...
 
Func< string?, ReadOnlyMemory
< byte >, long, Type,
IConvertible > 
ObjectToPrimitive = 64 [get, set]
 When you attempt to read a primitive (such as a string or double), but an object or a list is encountered instead, this property controls how that object is converted to the primitive type. If this property is left with its default value of null, an object or list cannot be converted to any primitive type, so FormatException is thrown instead. If you provide a delegate, it is given the property name and raw bytes of a UTF-8 JSON object or list, and whatever value it returns is the conversion result. More...
 
Func< string, ReadOnlyMemory
< byte >, bool, Memory< byte > > 
PrimitiveToObject = null [get, set]
 When you attempt to read an object or a list, but a primitive type is encountered instead, this property controls how that primitive is converted to an object or list (see remarks) More...
 
Func< string, string, bool,
Memory< byte > > 
StringToObject = null [get, set]
 When you attempt to read an object, but a string is encountered instead, this property controls how that string is converted to an object. If this property is left with its default value of null, a string cannot be converted to an object and FormatException is thrown instead. If you provide a delegate, it is given the property name and string, and it must return valid JSON that will be read instead. More...
 
Func< string, ReadOnlyMemory
< byte >, IConvertible > 
ObjectToNumber = null [get, set]
 
string TrueAsString = null [get, set]
 When attempting to read a boolean value, a string can be accepted instead if that string has the value specified here. More...
 
string FalseAsString = "true" [get, set]
 When attempting to read a boolean value, a string can be accepted instead if that string has the value specified here. More...
 
bool VerifyEof = "false" [get, set]
 When this property is true and the root object has been read successfully, the reader checks whether there is additional non-whitespace text beyond the end of what was read, and throws an exception if extra junk is encountered. More...
 
bool AllowMissingFields = true [get, set]
 This property controls Reader's behavior when a request is made to read a field that does not exist. If this property is true, the missing field is treated exactly as if it were set to null. Default: false. More...
 
bool ReadNullPrimitivesAsDefault = false [get, set]
 This property requests that if a property is set to null but read as a primitive type, the default value of that type should be returned instead of throwing an exception. For example, if Reader.Sync(FieldId, int) encounters a null, it will return 0 instead if throwing an exception if this property is true. More...
 

Property Documentation

bool SyncJson.Options.ForReader.AllowComments
getset

Whether to accept // and /* */ comments when reading JSON.

bool SyncJson.Options.ForReader.AllowMissingFields = true
getset

This property controls Reader's behavior when a request is made to read a field that does not exist. If this property is true, the missing field is treated exactly as if it were set to null. Default: false.

string SyncJson.Options.ForReader.FalseAsString = "true"
getset

When attempting to read a boolean value, a string can be accepted instead if that string has the value specified here.

int SyncJson.Options.ForReader.MaxDepth = false
getset

If objects are nested more than this many levels deep, an exception will be thrown and the reader will refuse to read further. This option helps avoid StackOverflowException while reading, which normally forces a .NET process to terminate.

Func<string?, ReadOnlyMemory<byte>, long, Type, IConvertible> SyncJson.Options.ForReader.ObjectToPrimitive = 64
getset

When you attempt to read a primitive (such as a string or double), but an object or a list is encountered instead, this property controls how that object is converted to the primitive type. If this property is left with its default value of null, an object or list cannot be converted to any primitive type, so FormatException is thrown instead. If you provide a delegate, it is given the property name and raw bytes of a UTF-8 JSON object or list, and whatever value it returns is the conversion result.

The first byte of the Memory buffer is '{' if the input is an object, or '[' if the input is a list.

One way of starting to handle the conversion request would be to call NewReader to begin parsing the memory buffer.

If the target type is a string, the simplest implementation is to return the JSON itself, which can be accomplished as follows:

// This code requires .NET Core 3+ (use json.ToArray() otherwise)
var options = new SyncJson.Options {
ReadObjectAsPrimitive = (name, json, t) => Encoding.UTF8.GetString(json.Span)
};
Func<string, ReadOnlyMemory<byte>, bool, Memory<byte> > SyncJson.Options.ForReader.PrimitiveToObject = null
getset

When you attempt to read an object or a list, but a primitive type is encountered instead, this property controls how that primitive is converted to an object or list (see remarks)

The boolean parameter is true when a list is required.

If this property is left with its default value of null, (1) If the JSON value is a string, ReadStringAsObject is used instead (2) If the JSON value is a number, ReadNumberAsObject is used instead (3) Otherwise, the value cannot be converted to an object or list, so FormatException is thrown.

bool SyncJson.Options.ForReader.ReadNullPrimitivesAsDefault = false
getset

This property requests that if a property is set to null but read as a primitive type, the default value of that type should be returned instead of throwing an exception. For example, if Reader.Sync(FieldId, int) encounters a null, it will return 0 instead if throwing an exception if this property is true.

See also
ObjectMode.ReadNullAsDefault
bool SyncJson.Options.ForReader.Strict = true
getset

Whether to follow JSON rules strictly when reading JSON. When this mode is disabled, the following syntax does not cause an exception: (1) a comma before a closing ']' or '}', (2) a number with leading '.' or leading '0' (with other digits), (3) the \0 (null character) escape sequence, (4) invalid escape sequences (instead, is read as if it were \q), (5) non-string object keys (which are essentially ignored).

The legality of comments and garbage after EOF is controlled independently with the AllowComments and VerifyEof properties.

Although JSON technically prohibits control characters, I felt it wasn't worth the performance cost of detecting control characters when decoding strings, so Strict mode allow them.

Func<string, string, bool, Memory<byte> > SyncJson.Options.ForReader.StringToObject = null
getset

When you attempt to read an object, but a string is encountered instead, this property controls how that string is converted to an object. If this property is left with its default value of null, a string cannot be converted to an object and FormatException is thrown instead. If you provide a delegate, it is given the property name and string, and it must return valid JSON that will be read instead.

string SyncJson.Options.ForReader.TrueAsString = null
getset

When attempting to read a boolean value, a string can be accepted instead if that string has the value specified here.

bool SyncJson.Options.ForReader.VerifyEof = "false"
getset

When this property is true and the root object has been read successfully, the reader checks whether there is additional non-whitespace text beyond the end of what was read, and throws an exception if extra junk is encountered.