Skip to content

Commit

Permalink
Complemented Fetch methods set
Browse files Browse the repository at this point in the history
  • Loading branch information
SinoAHpx committed Oct 10, 2024
1 parent df32b9e commit 4e5dc29
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 23 deletions.
22 changes: 3 additions & 19 deletions Technetium.Runner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Technetium.Text.String;

Expand Down Expand Up @@ -88,26 +89,9 @@ static async Task Main(string[] args)
while (true)
{
var path = Console.ReadLine();
//expect: me
var result = Fetch(b, path!);
Console.WriteLine(result);
var json = b.FetchInt32(path);
Console.WriteLine(json);
}

}

public static string? Fetch(string json, string path)
{
//Profile.Nest.Inside
var jDocument = JsonDocument.Parse(json);
if (!path.Contains('.'))
{
return jDocument.RootElement.GetProperty(path).GetRawText();
}
var segments = path.Split('.').ToList();
var stage = Fetch(Fetch(json, segments[0])!, path.Empty($"{segments[0]}."));
segments.RemoveAt(0);

return stage;
}

class MyClass
Expand Down
5 changes: 4 additions & 1 deletion Technetium.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AExceptionDispatchInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fbc1d76b9965449cf84649bc8d6b5b9c5c63600_003Fea_003Fb40176bb_003FExceptionDispatchInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonElement_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F226c29ed49e649b08fca33e404b62d2f181c00_003Ffb_003F76bb7ce8_003FJsonElement_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonNode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F226c29ed49e649b08fca33e404b62d2f181c00_003F49_003F122d7c12_003FJsonNode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANumber_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fbc1d76b9965449cf84649bc8d6b5b9c5c63600_003Fd8_003F81409932_003FNumber_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F226c29ed49e649b08fca33e404b62d2f181c00_003F59_003F0776af89_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String>
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">/Users/a1/Library/Caches/JetBrains/Rider2024.2/resharper-host/temp/Rider/vAny/CoverageData/_Technetium.-995326881/Snapshot/snapshot.utdcvr</s:String>

<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=11b0b4e4_002D0e7f_002D48bb_002Da5e1_002D6b5cb3a07667/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="TestSubstringBetween" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::EE9E784A-31A4-41D9-85B9-1A562BCE19F2::net8.0::Technetium.Test.Text.String.FluentStringTest.TestSubstringBetween&lt;/TestId&gt;
Expand Down
139 changes: 136 additions & 3 deletions Technetium/Text/String/JsonString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,142 @@ public static class JsonString
}
}

public static string? Fetch(this string json)
/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="jNode"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static JsonNode? FetchJsonNode(this JsonNode jNode, string path)
{
if (!path.Contains('.'))
{
return jNode[path];
}
var segments = path.Split('.').ToList();
var stage = FetchJsonNode(FetchJsonNode(jNode, segments[0])!, path.Empty($"{segments[0]}."));
segments.RemoveAt(0);

return stage;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
private static string? Fetch(this string json, string path)
{
var jsonNode = JsonNode.Parse(json)!;
return FetchJsonNode(jsonNode, path)?.ToString();
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static string? FetchString(this string json, string path)
{
return Fetch(json, path);
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static bool? FetchBoolean(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? bool.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static decimal? FetchDecimal(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? decimal.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static double? FetchDouble(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? double.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static Guid? FetchGuid(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? Guid.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static int? FetchInt32(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? int.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static long? FetchInt64(this string json, string path)
{
var jDocument = JsonDocument.Parse(json);
return jDocument.RootElement.GetProperty("Name").GetString();
var result = Fetch(json, path);
return result != null ? long.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static float? FetchSingle(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? float.Parse(result) : null;
}

/// <summary>
/// Example json: {"name": "x", "nest": {"name": "y"}}, so to get "y", path would be "nest.name"
/// </summary>
/// <param name="json"></param>
/// <param name="path">A combination of property name and dots</param>
/// <returns></returns>
public static DateTime? FetchDateTime(this string json, string path)
{
var result = Fetch(json, path);
return result != null ? DateTime.Parse(result) : null;
}

}

0 comments on commit 4e5dc29

Please sign in to comment.