Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code refactor #2973

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,15 @@ public BsonBinaryWriter(BinaryWriter writer)
_writer = writer;
}

public void Flush()
{
_writer.Flush();
}
public void Flush() => _writer.Flush();

public void Close()
{
public void Close() =>
#if HAVE_STREAM_READER_WRITER_CLOSE
_writer.Close();
#else
_writer.Dispose();
#endif
}


public void WriteToken(BsonToken t)
{
Expand Down
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Bson/BsonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ private class ContainerContext
public int Length;
public int Position;

public ContainerContext(BsonType type)
{
Type = type;
}
public ContainerContext(BsonType type) => Type = type;
}

/// <summary>
Expand Down
34 changes: 7 additions & 27 deletions Src/Newtonsoft.Json/Bson/BsonToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ public void Add(string name, BsonToken token)

public override BsonType Type => BsonType.Object;

public IEnumerator<BsonProperty> GetEnumerator()
{
return _children.GetEnumerator();
}
public IEnumerator<BsonProperty> GetEnumerator() => _children.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

internal class BsonArray : BsonToken, IEnumerable<BsonToken>
Expand All @@ -73,25 +67,17 @@ public void Add(BsonToken token)
public override BsonType Type => BsonType.Array;

public IEnumerator<BsonToken> GetEnumerator()
{
return _children.GetEnumerator();
}
=> _children.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

internal class BsonEmpty : BsonToken
{
public static readonly BsonToken Null = new BsonEmpty(BsonType.Null);
public static readonly BsonToken Undefined = new BsonEmpty(BsonType.Undefined);

private BsonEmpty(BsonType type)
{
Type = type;
}
private BsonEmpty(BsonType type) => Type = type;

public override BsonType Type { get; }
}
Expand Down Expand Up @@ -129,21 +115,15 @@ internal class BsonString : BsonValue
public bool IncludeLength { get; }

public BsonString(object value, bool includeLength)
: base(value, BsonType.String)
{
IncludeLength = includeLength;
}
: base(value, BsonType.String) => IncludeLength = includeLength;
}

internal class BsonBinary : BsonValue
{
public BsonBinaryType BinaryType { get; set; }

public BsonBinary(byte[] value, BsonBinaryType binaryType)
: base(value, BsonType.Binary)
{
BinaryType = binaryType;
}
: base(value, BsonType.Binary) => BinaryType = binaryType;
}

internal class BsonRegex : BsonToken
Expand Down
15 changes: 3 additions & 12 deletions Src/Newtonsoft.Json/Bson/BsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public BsonWriter(BinaryWriter writer)
/// <summary>
/// Flushes whatever is in the buffer to the underlying <see cref="Stream"/> and also flushes the underlying stream.
/// </summary>
public override void Flush()
{
_writer.Flush();
}
public override void Flush() => _writer.Flush();

/// <summary>
/// Writes the end.
Expand Down Expand Up @@ -193,15 +190,9 @@ private void AddParent(BsonToken container)
_parent = container;
}

private void RemoveParent()
{
_parent = _parent.Parent;
}
private void RemoveParent() => _parent = _parent.Parent;

private void AddValue(object value, BsonType type)
{
AddToken(new BsonValue(value, type));
}
private void AddValue(object value, BsonType type) => AddToken(new BsonValue(value, type));

internal void AddToken(BsonToken token)
{
Expand Down
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Converters/BsonObjectIdConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(BsonObjectId));
}
public override bool CanConvert(Type objectType) => (objectType == typeof(BsonObjectId));
}
}
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Converters/CustomCreationConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override bool CanConvert(Type objectType) => typeof(T).IsAssignableFrom(objectType);

/// <summary>
/// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
Expand Down
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Converters/DataSetConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
/// <returns>
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type valueType)
{
return typeof(DataSet).IsAssignableFrom(valueType);
}
public override bool CanConvert(Type valueType) => typeof(DataSet).IsAssignableFrom(valueType);
}
}

Expand Down
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Converters/DataTableConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,7 @@ private static Type GetColumnDataType(JsonReader reader)
/// <returns>
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type valueType)
{
return typeof(DataTable).IsAssignableFrom(valueType);
}
public override bool CanConvert(Type valueType) => typeof(DataTable).IsAssignableFrom(valueType);
}
}

Expand Down
6 changes: 2 additions & 4 deletions Src/Newtonsoft.Json/Converters/EntityKeyMemberConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,8 @@ private static void EnsureReflectionObject(Type objectType)
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return objectType.AssignableToTypeName(EntityKeyMemberFullTypeName, false);
}
public override bool CanConvert(Type objectType)
=> objectType.AssignableToTypeName(EntityKeyMemberFullTypeName, false);
}
}

Expand Down
11 changes: 3 additions & 8 deletions Src/Newtonsoft.Json/Converters/ExpandoObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
return ReadValue(reader);
}
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
=> ReadValue(reader);

private object? ReadValue(JsonReader reader)
{
Expand Down Expand Up @@ -147,10 +145,7 @@ private object ReadObject(JsonReader reader)
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(ExpandoObject));
}
public override bool CanConvert(Type objectType) => (objectType == typeof(ExpandoObject));

/// <summary>
/// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
Expand Down
15 changes: 3 additions & 12 deletions Src/Newtonsoft.Json/Converters/RegexConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
}
}

private bool HasFlag(RegexOptions options, RegexOptions flag)
{
return ((options & flag) == flag);
}
private bool HasFlag(RegexOptions options, RegexOptions flag) => ((options & flag) == flag);

#pragma warning disable 618
private void WriteBson(BsonWriter writer, Regex regex)
Expand Down Expand Up @@ -221,15 +218,9 @@ private Regex ReadRegexObject(JsonReader reader, JsonSerializer serializer)
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return objectType.Name == nameof(Regex) && IsRegex(objectType);
}
public override bool CanConvert(Type objectType) => objectType.Name == nameof(Regex) && IsRegex(objectType);

[MethodImpl(MethodImplOptions.NoInlining)]
private bool IsRegex(Type objectType)
{
return (objectType == typeof(Regex));
}
private bool IsRegex(Type objectType) => (objectType == typeof(Regex));
}
}
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Converters/UnixDateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ public UnixDateTimeConverter() : this(false)
/// <c>false</c> to throw an exception when a date being converted to or from JSON
/// occurred before Unix epoch. The default value is <c>false</c>.
/// </param>
public UnixDateTimeConverter(bool allowPreEpoch)
{
AllowPreEpoch = allowPreEpoch;
}
public UnixDateTimeConverter(bool allowPreEpoch) => AllowPreEpoch = allowPreEpoch;

/// <summary>
/// Writes the JSON representation of the object.
Expand Down
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/Converters/VersionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
/// <returns>
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Version);
}
public override bool CanConvert(Type objectType) => objectType == typeof(Version);
}
}
10 changes: 2 additions & 8 deletions Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2215,18 +2215,12 @@ public override bool CanConvert(Type valueType)

#if HAVE_XLINQ
[MethodImpl(MethodImplOptions.NoInlining)]
private bool IsXObject(Type valueType)
{
return typeof(XObject).IsAssignableFrom(valueType);
}
private bool IsXObject(Type valueType) => typeof(XObject).IsAssignableFrom(valueType);
#endif

#if HAVE_XML_DOCUMENT
[MethodImpl(MethodImplOptions.NoInlining)]
private bool IsXmlNode(Type valueType)
{
return typeof(XmlNode).IsAssignableFrom(valueType);
}
private bool IsXmlNode(Type valueType) => typeof(XmlNode).IsAssignableFrom(valueType);
#endif
}
}
Expand Down
10 changes: 2 additions & 8 deletions Src/Newtonsoft.Json/DefaultJsonNameTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,12 @@ public class DefaultJsonNameTable : JsonNameTable
private Entry[] _entries;
private int _mask = 31;

static DefaultJsonNameTable()
{
HashCodeRandomizer = Environment.TickCount;
}
static DefaultJsonNameTable() => HashCodeRandomizer = Environment.TickCount;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultJsonNameTable"/> class.
/// </summary>
public DefaultJsonNameTable()
{
_entries = new Entry[_mask + 1];
}
public DefaultJsonNameTable() => _entries = new Entry[_mask + 1];

/// <summary>
/// Gets a string containing the same characters as the specified range of characters in the given array.
Expand Down
5 changes: 1 addition & 4 deletions Src/Newtonsoft.Json/JsonArrayAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public JsonArrayAttribute()
/// Initializes a new instance of the <see cref="JsonObjectAttribute"/> class with a flag indicating whether the array can contain null items.
/// </summary>
/// <param name="allowNullItems">A flag indicating whether the array can contain null items.</param>
public JsonArrayAttribute(bool allowNullItems)
{
_allowNullItems = allowNullItems;
}
public JsonArrayAttribute(bool allowNullItems) => _allowNullItems = allowNullItems;

/// <summary>
/// Initializes a new instance of the <see cref="JsonArrayAttribute"/> class with the specified container Id.
Expand Down
Loading
Loading