Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Added new extension for Newtonsoft.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
AptiviCEO committed Jul 22, 2021
1 parent aabd570 commit 234f63a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Extensification.External.Tests/Newtonsoft.Json/JProperty.vb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ Public Class JPropertyTests
Dim JsonProperty As String = JsonToken.First.GetPropertyNameContaining("sion")
Assert.AreEqual("", JsonProperty)
End Sub

''' <summary>
''' Tests the logic of getting properties that has the specific type in value
''' </summary>
<TestMethod>
Sub TestGetPropertiesTypeInValue()
Dim JsonString As String = My.Resources.JSON_PropertyTest1
Dim JsonToken As JToken = JToken.Parse(JsonString)
Dim JsonProperty As List(Of JToken) = JsonToken.First.GetPropertiesTypeInValue(JTokenType.String)
Assert.IsTrue(JsonProperty.Count <> 0)
End Sub

''' <summary>
''' Tests the logic of getting properties that has the specific type in value should return nothing if the property of specific type doesn't exist
''' </summary>
<TestMethod>
Sub TestGetPropertiesTypeInValueShouldReturnNothingIfNotExists()
Dim JsonString As String = My.Resources.JSON_PropertyTest1
Dim JsonToken As JToken = JToken.Parse(JsonString)
Dim JsonProperty As List(Of JToken) = JsonToken.First.GetPropertiesTypeInValue(JTokenType.Boolean)
Assert.IsTrue(JsonProperty.Count = 0)
End Sub

''' <summary>
''' Tests the logic of getting properties that has the specific type in value should return everything if the type is set to <see cref="JTokenType.None"/>
''' </summary>
<TestMethod>
Sub TestGetPropertiesTypeInValueShouldReturnEverythingIfNoTokenTypeSpecified()
Dim JsonString As String = My.Resources.JSON_PropertyTest1
Dim JsonToken As JToken = JToken.Parse(JsonString)
Dim JsonProperty As List(Of JToken) = JsonToken.First.GetPropertiesTypeInValue(JTokenType.None)
Assert.IsTrue(JsonProperty.Count = 4)
End Sub
#End Region

#Region "Querying"
Expand Down
1 change: 1 addition & 0 deletions Extensification.External/Extensification.External.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This package is for extensions for external libraries.</Description>
<PackageTags>extensions, extension methods</PackageTags>
<PackageReleaseNotes>2021.6:
- Broke the code to separate code files
- Added new extension

2021.4.1:
- Fixed no code documentation
Expand Down
17 changes: 17 additions & 0 deletions Extensification.External/Newtonsoft.Json/JProperty/Getting.vb
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,22 @@ Namespace Newtonsoft.Json.JPropertyExts
Return ""
End Function

''' <summary>
''' Gets properties that have the specific type in value.
''' </summary>
''' <param name="Token">JSON token</param>
''' <param name="Type">JSON token type to search. If the type is <see cref="JTokenType.None"/>, returns all properties.</param>
''' <returns>A property list if properties with specific type is found; empty list if nothing is found</returns>
<Extension>
Public Function GetPropertiesTypeInValue(ByVal Token As JToken, ByVal Type As JTokenType) As List(Of JToken)
Dim TokenList As New List(Of JToken)
For Each TokenProperty As JProperty In Token
If TokenProperty.Value.Type = Type Or Type = JTokenType.None Then
TokenList.Add(TokenProperty)
End If
Next
Return TokenList
End Function

End Module
End Namespace
2 changes: 2 additions & 0 deletions Extensification/Extensification.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<PackageTags>extensions, extension methods</PackageTags>
<PackageReleaseNotes>2021.6:
- Added SByte support for Byte extension
- Added Enumerable extensions
- Incompatible types fixed

2021.5.2:
- Added new dictionary extensions
Expand Down

0 comments on commit 234f63a

Please sign in to comment.