summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPiotr Fusik <piotr@fusion-lang.org>2024-02-16 16:54:16 +0100
committerPiotr Fusik <piotr@fusion-lang.org>2024-02-16 16:54:16 +0100
commit16cdca9616f6c80a60b52b92ac02d4cc3aa39345 (patch)
treee662912ee54308b85b1e2bf11d7875cf8ff79c07
parent464803d2bb297fcd33e7853ee307bb4029867b72 (diff)
[json] JsonElement.Parse(str) in C#.
#140
-rw-r--r--AST.fu38
-rw-r--r--GenCs.fu20
-rw-r--r--Makefile2
-rw-r--r--editors/notepad-plus-plus/Fusion.udl.xml2
-rw-r--r--editors/vscode/syntaxes/fusion.tmLanguage.json2
-rw-r--r--libfut.cpp82
-rw-r--r--libfut.cs56
-rw-r--r--libfut.hpp12
-rw-r--r--libfut.js286
-rw-r--r--test/JsonElement.fu38
10 files changed, 410 insertions, 128 deletions
diff --git a/AST.fu b/AST.fu
index dc024be..962b02f 100644
--- a/AST.fu
+++ b/AST.fu
@@ -97,6 +97,8 @@ public enum FuId
RegexOptionsEnum,
RegexClass,
MatchClass,
+ JsonValueKindEnum,
+ JsonElementClass,
LockClass,
StringLength,
ArrayLength,
@@ -107,6 +109,7 @@ public enum FuId
MatchEnd,
MatchLength,
MatchValue,
+ JsonElementValueKind,
MathNaN,
MathNegativeInfinity,
MathPositiveInfinity,
@@ -198,6 +201,13 @@ public enum FuId
MatchFindStr,
MatchFindRegex,
MatchGetCapture,
+ JsonElementParse,
+ JsonElementTryParse,
+ JsonElementGetObject,
+ JsonElementGetArray,
+ JsonElementGetString,
+ JsonElementGetDouble,
+ JsonElementGetBoolean,
MathMethod,
MathAbs,
MathCeiling,
@@ -1464,12 +1474,13 @@ public class FuSystem : FuScope
set.AddMethod(VoidType, removeId, "Remove", true, FuVar.New(TypeParam0, "value"));
}
- void AddDictionary!(FuId id, string name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId)
+ FuClass AddDictionary!(FuId id, string name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId)
{
FuClass! dict = AddCollection(id, name, 2, clearId, countId);
dict.Add(FuMethod.New(dict, FuVisibility.FinalValueType, FuCallType.Normal, VoidType, FuId.DictionaryAdd, "Add", true, FuVar.New(TypeParam0, "key")));
dict.AddMethod(BoolType, containsKeyId, "ContainsKey", false, FuVar.New(TypeParam0, "key"));
dict.AddMethod(VoidType, removeId, "Remove", true, FuVar.New(TypeParam0, "key"));
+ return dict;
}
static void AddEnumValue(FuEnum# enu, FuConst# value)
@@ -1585,7 +1596,7 @@ public class FuSystem : FuScope
stackClass.AddMethod(TypeParam0, FuId.StackPop, "Pop", true);
AddSet(FuId.HashSetClass, "HashSet", FuId.HashSetAdd, FuId.HashSetClear, FuId.HashSetContains, FuId.HashSetCount, FuId.HashSetRemove);
AddSet(FuId.SortedSetClass, "SortedSet", FuId.SortedSetAdd, FuId.SortedSetClear, FuId.SortedSetContains, FuId.SortedSetCount, FuId.SortedSetRemove);
- AddDictionary(FuId.DictionaryClass, "Dictionary", FuId.DictionaryClear, FuId.DictionaryContainsKey, FuId.DictionaryCount, FuId.DictionaryRemove);
+ FuClass dictionaryClass = AddDictionary(FuId.DictionaryClass, "Dictionary", FuId.DictionaryClear, FuId.DictionaryContainsKey, FuId.DictionaryCount, FuId.DictionaryRemove);
AddDictionary(FuId.SortedDictionaryClass, "SortedDictionary", FuId.SortedDictionaryClear, FuId.SortedDictionaryContainsKey, FuId.SortedDictionaryCount, FuId.SortedDictionaryRemove);
AddDictionary(FuId.OrderedDictionaryClass, "OrderedDictionary", FuId.OrderedDictionaryClear, FuId.OrderedDictionaryContainsKey, FuId.OrderedDictionaryCount, FuId.OrderedDictionaryRemove);
@@ -1648,6 +1659,29 @@ public class FuSystem : FuScope
matchClass.Add(FuProperty.New(UIntType, FuId.MatchLength, "Length"));
matchClass.Add(FuProperty.New(StringStorageType, FuId.MatchValue, "Value"));
Add(matchClass);
+ FuEnum# jsonValueKindEnum = NewEnum(false);
+ jsonValueKindEnum.IsPublic = true;
+ jsonValueKindEnum.Id = FuId.JsonValueKindEnum;
+ jsonValueKindEnum.Name = "JsonValueKind";
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Object", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Array", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "String", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Number", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "True", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "False", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Null", VisitStatus = FuVisitStatus.Done });
+ Add(jsonValueKindEnum);
+ FuClass# jsonElementClass = FuClass.New(FuCallType.Sealed, FuId.JsonElementClass, "JsonElement");
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, VoidType, FuId.JsonElementParse, "Parse", true, FuVar.New(StringPtrType, "value")));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, BoolType, FuId.JsonElementTryParse, "TryParse", true, FuVar.New(StringPtrType, "value")));
+ FuDynamicPtrType# jsonElementPtr = new FuDynamicPtrType { Class = jsonElementClass };
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, new FuClassType { Class = dictionaryClass, TypeArg0 = StringStorageType, TypeArg1 = jsonElementPtr }, FuId.JsonElementGetObject, "GetObject", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, new FuClassType { Class = listClass, TypeArg0 = jsonElementPtr }, FuId.JsonElementGetArray, "GetArray", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, StringPtrType, FuId.JsonElementGetString, "GetString", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, DoubleType, FuId.JsonElementGetDouble, "GetDouble", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, BoolType, FuId.JsonElementGetBoolean, "GetBoolean", false));
+ jsonElementClass.Add(FuProperty.New(jsonValueKindEnum, FuId.JsonElementValueKind, "ValueKind"));
+ Add(jsonElementClass);
FuFloatingType# floatIntType = new FuFloatingType { Id = FuId.FloatIntType, Name = "float" };
FuClass# mathClass = FuClass.New(FuCallType.Static, FuId.None, "Math");
diff --git a/GenCs.fu b/GenCs.fu
index a703443..a5b1a1e 100644
--- a/GenCs.fu
+++ b/GenCs.fu
@@ -1,6 +1,6 @@
// GenCs.fu - C# code generator
//
-// Copyright (C) 2011-2023 Piotr Fusik
+// Copyright (C) 2011-2024 Piotr Fusik
//
// This file is part of Fusion Transpiler,
// see https://github.com/fusionlanguage/fut
@@ -267,6 +267,10 @@ public class GenCs : GenTyped
Include("System.Text.RegularExpressions");
Write(klass.Class.Name);
break;
+ case FuId.JsonElementClass:
+ Include("System.Text.Json");
+ Write("JsonElement");
+ break;
case FuId.LockClass:
Write("object");
break;
@@ -659,6 +663,20 @@ public class GenCs : GenTyped
args[0].Accept(this, FuPriority.Argument);
Write("].Value");
break;
+ case FuId.JsonElementParse:
+ obj.Accept(this, FuPriority.Assign);
+ Write(" = JsonDocument.Parse(");
+ args[0].Accept(this, FuPriority.Argument);
+ Write(").RootElement");
+ break;
+ case FuId.JsonElementGetObject:
+ Include("System.Linq");
+ WritePostfix(obj, ".EnumerateObject().ToDictionary(p => p.Name, p => p.Value)");
+ break;
+ case FuId.JsonElementGetArray:
+ Include("System.Linq");
+ WritePostfix(obj, ".EnumerateArray().ToList()");
+ break;
case FuId.MathMethod:
case FuId.MathAbs:
case FuId.MathCeiling:
diff --git a/Makefile b/Makefile
index 76c20c4..a30b441 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ CXXFLAGS = -Wall -O2
DOTNET_BASE_DIR := $(shell dotnet --info 2>/dev/null | sed -n 's/ Base Path: //p')
ifdef DOTNET_BASE_DIR
DOTNET_REF_DIR := $(shell realpath '$(DOTNET_BASE_DIR)../../packs/Microsoft.NETCore.App.Ref'/*/ref/net* | head -1)
-CSC := dotnet '$(DOTNET_BASE_DIR)Roslyn/bincore/csc.dll' -nologo $(patsubst %,'-r:$(DOTNET_REF_DIR)/System.%.dll', Collections Collections.Specialized Console Linq Runtime Text.RegularExpressions Threading)
+CSC := dotnet '$(DOTNET_BASE_DIR)Roslyn/bincore/csc.dll' -nologo $(patsubst %,'-r:$(DOTNET_REF_DIR)/System.%.dll', Collections Collections.Specialized Console Linq Memory Runtime Text.Json Text.RegularExpressions Threading)
endif
JAVAC = javac
TEST_CFLAGS = -Wall -Werror
diff --git a/editors/notepad-plus-plus/Fusion.udl.xml b/editors/notepad-plus-plus/Fusion.udl.xml
index 4251c68..8469fb3 100644
--- a/editors/notepad-plus-plus/Fusion.udl.xml
+++ b/editors/notepad-plus-plus/Fusion.udl.xml
@@ -26,7 +26,7 @@
<Keywords name="Folders in comment, close"></Keywords>
<Keywords name="Keywords1">abstract assert base break case class const continue default do else enum false for foreach if in internal is lock native new null override protected public resource return sealed static switch this throw throws true virtual when while</Keywords>
<Keywords name="Keywords2">bool byte double float int long short string uint ushort void</Keywords>
- <Keywords name="Keywords3">Console Convert Dictionary Encoding Environment Exception HashSet List Lock Match Math OrderedDictionary Queue Regex RegexOptions SortedDictionary SortedSet Stack StringWriter TextWriter</Keywords>
+ <Keywords name="Keywords3">Console Convert Dictionary Encoding Environment Exception HashSet JsonElement List Lock Match Math OrderedDictionary Queue Regex RegexOptions SortedDictionary SortedSet Stack StringWriter TextWriter</Keywords>
<Keywords name="Keywords4">#elif #else #endif #if</Keywords>
<Keywords name="Keywords5"></Keywords>
<Keywords name="Keywords6"></Keywords>
diff --git a/editors/vscode/syntaxes/fusion.tmLanguage.json b/editors/vscode/syntaxes/fusion.tmLanguage.json
index 84ceab5..fd9c28c 100644
--- a/editors/vscode/syntaxes/fusion.tmLanguage.json
+++ b/editors/vscode/syntaxes/fusion.tmLanguage.json
@@ -890,7 +890,7 @@
}
},
"class-builtin": {
- "match": "\\b(Console|Convert|Dictionary|Encoding|Environment|Exception|HashSet|List|Lock|Match|Math|Regex|RegexOptions|SortedDictionary|SortedSet|Stack|StringWriter|TextWriter)\\b",
+ "match": "\\b(Console|Convert|Dictionary|Encoding|Environment|Exception|HashSet|JsonElement|List|Lock|Match|Math|Regex|RegexOptions|SortedDictionary|SortedSet|Stack|StringWriter|TextWriter)\\b",
"captures": {
"1": {
"name": "support.class.fu"
diff --git a/libfut.cpp b/libfut.cpp
index 57ece61..44c4294 100644
--- a/libfut.cpp
+++ b/libfut.cpp
@@ -2520,7 +2520,7 @@ FuSystem::FuSystem()
stackClass->addMethod(this->typeParam0, FuId::stackPop, "Pop", true);
addSet(FuId::hashSetClass, "HashSet", FuId::hashSetAdd, FuId::hashSetClear, FuId::hashSetContains, FuId::hashSetCount, FuId::hashSetRemove);
addSet(FuId::sortedSetClass, "SortedSet", FuId::sortedSetAdd, FuId::sortedSetClear, FuId::sortedSetContains, FuId::sortedSetCount, FuId::sortedSetRemove);
- addDictionary(FuId::dictionaryClass, "Dictionary", FuId::dictionaryClear, FuId::dictionaryContainsKey, FuId::dictionaryCount, FuId::dictionaryRemove);
+ const FuClass * dictionaryClass = addDictionary(FuId::dictionaryClass, "Dictionary", FuId::dictionaryClear, FuId::dictionaryContainsKey, FuId::dictionaryCount, FuId::dictionaryRemove);
addDictionary(FuId::sortedDictionaryClass, "SortedDictionary", FuId::sortedDictionaryClear, FuId::sortedDictionaryContainsKey, FuId::sortedDictionaryCount, FuId::sortedDictionaryRemove);
addDictionary(FuId::orderedDictionaryClass, "OrderedDictionary", FuId::orderedDictionaryClear, FuId::orderedDictionaryContainsKey, FuId::orderedDictionaryCount, FuId::orderedDictionaryRemove);
std::shared_ptr<FuClass> textWriterClass = FuClass::new_(FuCallType::normal, FuId::textWriterClass, "TextWriter");
@@ -2590,6 +2590,65 @@ FuSystem::FuSystem()
matchClass->add(FuProperty::new_(this->uIntType, FuId::matchLength, "Length"));
matchClass->add(FuProperty::new_(this->stringStorageType, FuId::matchValue, "Value"));
add(matchClass);
+ std::shared_ptr<FuEnum> jsonValueKindEnum = newEnum(false);
+ jsonValueKindEnum->isPublic = true;
+ jsonValueKindEnum->id = FuId::jsonValueKindEnum;
+ jsonValueKindEnum->name = "JsonValueKind";
+ std::shared_ptr<FuConst> futemp9 = std::make_shared<FuConst>();
+ futemp9->visibility = FuVisibility::public_;
+ futemp9->name = "Object";
+ futemp9->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp9);
+ std::shared_ptr<FuConst> futemp10 = std::make_shared<FuConst>();
+ futemp10->visibility = FuVisibility::public_;
+ futemp10->name = "Array";
+ futemp10->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp10);
+ std::shared_ptr<FuConst> futemp11 = std::make_shared<FuConst>();
+ futemp11->visibility = FuVisibility::public_;
+ futemp11->name = "String";
+ futemp11->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp11);
+ std::shared_ptr<FuConst> futemp12 = std::make_shared<FuConst>();
+ futemp12->visibility = FuVisibility::public_;
+ futemp12->name = "Number";
+ futemp12->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp12);
+ std::shared_ptr<FuConst> futemp13 = std::make_shared<FuConst>();
+ futemp13->visibility = FuVisibility::public_;
+ futemp13->name = "True";
+ futemp13->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp13);
+ std::shared_ptr<FuConst> futemp14 = std::make_shared<FuConst>();
+ futemp14->visibility = FuVisibility::public_;
+ futemp14->name = "False";
+ futemp14->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp14);
+ std::shared_ptr<FuConst> futemp15 = std::make_shared<FuConst>();
+ futemp15->visibility = FuVisibility::public_;
+ futemp15->name = "Null";
+ futemp15->visitStatus = FuVisitStatus::done;
+ addEnumValue(jsonValueKindEnum, futemp15);
+ add(jsonValueKindEnum);
+ std::shared_ptr<FuClass> jsonElementClass = FuClass::new_(FuCallType::sealed, FuId::jsonElementClass, "JsonElement");
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, this->voidType, FuId::jsonElementParse, "Parse", true, FuVar::new_(this->stringPtrType, "value")));
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, this->boolType, FuId::jsonElementTryParse, "TryParse", true, FuVar::new_(this->stringPtrType, "value")));
+ std::shared_ptr<FuDynamicPtrType> jsonElementPtr = std::make_shared<FuDynamicPtrType>();
+ jsonElementPtr->class_ = jsonElementClass.get();
+ std::shared_ptr<FuClassType> futemp16 = std::make_shared<FuClassType>();
+ futemp16->class_ = dictionaryClass;
+ futemp16->typeArg0 = this->stringStorageType;
+ futemp16->typeArg1 = jsonElementPtr;
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, futemp16, FuId::jsonElementGetObject, "GetObject", false));
+ std::shared_ptr<FuClassType> futemp17 = std::make_shared<FuClassType>();
+ futemp17->class_ = listClass;
+ futemp17->typeArg0 = jsonElementPtr;
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, futemp17, FuId::jsonElementGetArray, "GetArray", false));
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, this->stringPtrType, FuId::jsonElementGetString, "GetString", false));
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, this->doubleType, FuId::jsonElementGetDouble, "GetDouble", false));
+ jsonElementClass->add(FuMethod::new_(nullptr, FuVisibility::public_, FuCallType::normal, this->boolType, FuId::jsonElementGetBoolean, "GetBoolean", false));
+ jsonElementClass->add(FuProperty::new_(jsonValueKindEnum, FuId::jsonElementValueKind, "ValueKind"));
+ add(jsonElementClass);
std::shared_ptr<FuFloatingType> floatIntType = std::make_shared<FuFloatingType>();
floatIntType->id = FuId::floatIntType;
floatIntType->name = "float";
@@ -2699,12 +2758,13 @@ void FuSystem::addSet(FuId id, std::string_view name, FuId addId, FuId clearId,
set->addMethod(this->voidType, removeId, "Remove", true, FuVar::new_(this->typeParam0, "value"));
}
-void FuSystem::addDictionary(FuId id, std::string_view name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId)
+const FuClass * FuSystem::addDictionary(FuId id, std::string_view name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId)
{
FuClass * dict = addCollection(id, name, 2, clearId, countId);
dict->add(FuMethod::new_(dict, FuVisibility::finalValueType, FuCallType::normal, this->voidType, FuId::dictionaryAdd, "Add", true, FuVar::new_(this->typeParam0, "key")));
dict->addMethod(this->boolType, containsKeyId, "ContainsKey", false, FuVar::new_(this->typeParam0, "key"));
dict->addMethod(this->voidType, removeId, "Remove", true, FuVar::new_(this->typeParam0, "key"));
+ return dict;
}
void FuSystem::addEnumValue(std::shared_ptr<FuEnum> enu, std::shared_ptr<FuConst> value)
@@ -15065,6 +15125,10 @@ void GenCs::writeType(const FuType * type, bool promote)
include("System.Text.RegularExpressions");
write(klass->class_->name);
break;
+ case FuId::jsonElementClass:
+ include("System.Text.Json");
+ write("JsonElement");
+ break;
case FuId::lockClass:
write("object");
break;
@@ -15474,6 +15538,20 @@ void GenCs::writeCallExpr(const FuExpr * obj, const FuMethod * method, const std
(*args)[0]->accept(this, FuPriority::argument);
write("].Value");
break;
+ case FuId::jsonElementParse:
+ obj->accept(this, FuPriority::assign);
+ write(" = JsonDocument.Parse(");
+ (*args)[0]->accept(this, FuPriority::argument);
+ write(").RootElement");
+ break;
+ case FuId::jsonElementGetObject:
+ include("System.Linq");
+ writePostfix(obj, ".EnumerateObject().ToDictionary(p => p.Name, p => p.Value)");
+ break;
+ case FuId::jsonElementGetArray:
+ include("System.Linq");
+ writePostfix(obj, ".EnumerateArray().ToList()");
+ break;
case FuId::mathMethod:
case FuId::mathAbs:
case FuId::mathCeiling:
diff --git a/libfut.cs b/libfut.cs
index eee9825..05feb18 100644
--- a/libfut.cs
+++ b/libfut.cs
@@ -1316,6 +1316,8 @@ namespace Fusion
RegexOptionsEnum,
RegexClass,
MatchClass,
+ JsonValueKindEnum,
+ JsonElementClass,
LockClass,
StringLength,
ArrayLength,
@@ -1326,6 +1328,7 @@ namespace Fusion
MatchEnd,
MatchLength,
MatchValue,
+ JsonElementValueKind,
MathNaN,
MathNegativeInfinity,
MathPositiveInfinity,
@@ -1417,6 +1420,13 @@ namespace Fusion
MatchFindStr,
MatchFindRegex,
MatchGetCapture,
+ JsonElementParse,
+ JsonElementTryParse,
+ JsonElementGetObject,
+ JsonElementGetArray,
+ JsonElementGetString,
+ JsonElementGetDouble,
+ JsonElementGetBoolean,
MathMethod,
MathAbs,
MathCeiling,
@@ -3051,7 +3061,7 @@ namespace Fusion
stackClass.AddMethod(this.TypeParam0, FuId.StackPop, "Pop", true);
AddSet(FuId.HashSetClass, "HashSet", FuId.HashSetAdd, FuId.HashSetClear, FuId.HashSetContains, FuId.HashSetCount, FuId.HashSetRemove);
AddSet(FuId.SortedSetClass, "SortedSet", FuId.SortedSetAdd, FuId.SortedSetClear, FuId.SortedSetContains, FuId.SortedSetCount, FuId.SortedSetRemove);
- AddDictionary(FuId.DictionaryClass, "Dictionary", FuId.DictionaryClear, FuId.DictionaryContainsKey, FuId.DictionaryCount, FuId.DictionaryRemove);
+ FuClass dictionaryClass = AddDictionary(FuId.DictionaryClass, "Dictionary", FuId.DictionaryClear, FuId.DictionaryContainsKey, FuId.DictionaryCount, FuId.DictionaryRemove);
AddDictionary(FuId.SortedDictionaryClass, "SortedDictionary", FuId.SortedDictionaryClear, FuId.SortedDictionaryContainsKey, FuId.SortedDictionaryCount, FuId.SortedDictionaryRemove);
AddDictionary(FuId.OrderedDictionaryClass, "OrderedDictionary", FuId.OrderedDictionaryClear, FuId.OrderedDictionaryContainsKey, FuId.OrderedDictionaryCount, FuId.OrderedDictionaryRemove);
FuClass textWriterClass = FuClass.New(FuCallType.Normal, FuId.TextWriterClass, "TextWriter");
@@ -3106,6 +3116,29 @@ namespace Fusion
matchClass.Add(FuProperty.New(this.UIntType, FuId.MatchLength, "Length"));
matchClass.Add(FuProperty.New(this.StringStorageType, FuId.MatchValue, "Value"));
Add(matchClass);
+ FuEnum jsonValueKindEnum = NewEnum(false);
+ jsonValueKindEnum.IsPublic = true;
+ jsonValueKindEnum.Id = FuId.JsonValueKindEnum;
+ jsonValueKindEnum.Name = "JsonValueKind";
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Object", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Array", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "String", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Number", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "True", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "False", VisitStatus = FuVisitStatus.Done });
+ AddEnumValue(jsonValueKindEnum, new FuConst { Visibility = FuVisibility.Public, Name = "Null", VisitStatus = FuVisitStatus.Done });
+ Add(jsonValueKindEnum);
+ FuClass jsonElementClass = FuClass.New(FuCallType.Sealed, FuId.JsonElementClass, "JsonElement");
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, this.VoidType, FuId.JsonElementParse, "Parse", true, FuVar.New(this.StringPtrType, "value")));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, this.BoolType, FuId.JsonElementTryParse, "TryParse", true, FuVar.New(this.StringPtrType, "value")));
+ FuDynamicPtrType jsonElementPtr = new FuDynamicPtrType { Class = jsonElementClass };
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, new FuClassType { Class = dictionaryClass, TypeArg0 = this.StringStorageType, TypeArg1 = jsonElementPtr }, FuId.JsonElementGetObject, "GetObject", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, new FuClassType { Class = listClass, TypeArg0 = jsonElementPtr }, FuId.JsonElementGetArray, "GetArray", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, this.StringPtrType, FuId.JsonElementGetString, "GetString", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, this.DoubleType, FuId.JsonElementGetDouble, "GetDouble", false));
+ jsonElementClass.Add(FuMethod.New(null, FuVisibility.Public, FuCallType.Normal, this.BoolType, FuId.JsonElementGetBoolean, "GetBoolean", false));
+ jsonElementClass.Add(FuProperty.New(jsonValueKindEnum, FuId.JsonElementValueKind, "ValueKind"));
+ Add(jsonElementClass);
FuFloatingType floatIntType = new FuFloatingType { Id = FuId.FloatIntType, Name = "float" };
FuClass mathClass = FuClass.New(FuCallType.Static, FuId.None, "Math");
mathClass.Add(FuMethodGroup.New(FuMethod.New(null, FuVisibility.Public, FuCallType.Static, this.IntType, FuId.MathAbs, "Abs", false, FuVar.New(this.LongType, "a")), FuMethod.New(null, FuVisibility.Public, FuCallType.Static, this.FloatType, FuId.MathAbs, "Abs", false, FuVar.New(this.DoubleType, "a"))));
@@ -3242,12 +3275,13 @@ namespace Fusion
set.AddMethod(this.VoidType, removeId, "Remove", true, FuVar.New(this.TypeParam0, "value"));
}
- void AddDictionary(FuId id, string name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId)
+ FuClass AddDictionary(FuId id, string name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId)
{
FuClass dict = AddCollection(id, name, 2, clearId, countId);
dict.Add(FuMethod.New(dict, FuVisibility.FinalValueType, FuCallType.Normal, this.VoidType, FuId.DictionaryAdd, "Add", true, FuVar.New(this.TypeParam0, "key")));
dict.AddMethod(this.BoolType, containsKeyId, "ContainsKey", false, FuVar.New(this.TypeParam0, "key"));
dict.AddMethod(this.VoidType, removeId, "Remove", true, FuVar.New(this.TypeParam0, "key"));
+ return dict;
}
static void AddEnumValue(FuEnum enu, FuConst value)
@@ -15483,6 +15517,10 @@ namespace Fusion
Include("System.Text.RegularExpressions");
Write(klass.Class.Name);
break;
+ case FuId.JsonElementClass:
+ Include("System.Text.Json");
+ Write("JsonElement");
+ break;
case FuId.LockClass:
Write("object");
break;
@@ -15872,6 +15910,20 @@ namespace Fusion
args[0].Accept(this, FuPriority.Argument);
Write("].Value");
break;
+ case FuId.JsonElementParse:
+ obj.Accept(this, FuPriority.Assign);
+ Write(" = JsonDocument.Parse(");
+ args[0].Accept(this, FuPriority.Argument);
+ Write(").RootElement");
+ break;
+ case FuId.JsonElementGetObject:
+ Include("System.Linq");
+ WritePostfix(obj, ".EnumerateObject().ToDictionary(p => p.Name, p => p.Value)");
+ break;
+ case FuId.JsonElementGetArray:
+ Include("System.Linq");
+ WritePostfix(obj, ".EnumerateArray().ToList()");
+ break;
case FuId.MathMethod:
case FuId.MathAbs:
case FuId.MathCeiling:
diff --git a/libfut.hpp b/libfut.hpp
index f007309..693aa10 100644
--- a/libfut.hpp
+++ b/libfut.hpp
@@ -225,6 +225,8 @@ enum class FuId
regexOptionsEnum,
regexClass,
matchClass,
+ jsonValueKindEnum,
+ jsonElementClass,
lockClass,
stringLength,
arrayLength,
@@ -235,6 +237,7 @@ enum class FuId
matchEnd,
matchLength,
matchValue,
+ jsonElementValueKind,
mathNaN,
mathNegativeInfinity,
mathPositiveInfinity,
@@ -326,6 +329,13 @@ enum class FuId
matchFindStr,
matchFindRegex,
matchGetCapture,
+ jsonElementParse,
+ jsonElementTryParse,
+ jsonElementGetObject,
+ jsonElementGetArray,
+ jsonElementGetString,
+ jsonElementGetDouble,
+ jsonElementGetBoolean,
mathMethod,
mathAbs,
mathCeiling,
@@ -1473,7 +1483,7 @@ private:
std::shared_ptr<FuClass> stringClass = FuClass::new_(FuCallType::normal, FuId::stringClass, "string");
FuClass * addCollection(FuId id, std::string_view name, int typeParameterCount, FuId clearId, FuId countId);
void addSet(FuId id, std::string_view name, FuId addId, FuId clearId, FuId containsId, FuId countId, FuId removeId);
- void addDictionary(FuId id, std::string_view name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId);
+ const FuClass * addDictionary(FuId id, std::string_view name, FuId clearId, FuId containsKeyId, FuId countId, FuId removeId);
static void addEnumValue(std::shared_ptr<FuEnum> enu, std::shared_ptr<FuConst> value);
std::shared_ptr<FuConst> newConstLong(std::string_view name, int64_t value) const;
std::shared_ptr<FuConst> newConstDouble(std::string_view name, double value) const;
diff --git a/libfut.js b/libfut.js
index 2da4917..2772e10 100644
--- a/libfut.js
+++ b/libfut.js
@@ -1299,122 +1299,132 @@ export const FuId = {
REGEX_OPTIONS_ENUM : 34,
REGEX_CLASS : 35,
MATCH_CLASS : 36,
- LOCK_CLASS : 37,
- STRING_LENGTH : 38,
- ARRAY_LENGTH : 39,
- CONSOLE_ERROR : 40,
- MAIN : 41,
- CLASS_TO_STRING : 42,
- MATCH_START : 43,
- MATCH_END : 44,
- MATCH_LENGTH : 45,
- MATCH_VALUE : 46,
- MATH_NA_N : 47,
- MATH_NEGATIVE_INFINITY : 48,
- MATH_POSITIVE_INFINITY : 49,
- ENUM_FROM_INT : 50,
- ENUM_HAS_FLAG : 51,
- INT_TRY_PARSE : 52,
- LONG_TRY_PARSE : 53,
- DOUBLE_TRY_PARSE : 54,
- STRING_CONTAINS : 55,
- STRING_ENDS_WITH : 56,
- STRING_INDEX_OF : 57,
- STRING_LAST_INDEX_OF : 58,
- STRING_REPLACE : 59,
- STRING_STARTS_WITH : 60,
- STRING_SUBSTRING : 61,
- ARRAY_BINARY_SEARCH_ALL : 62,
- ARRAY_BINARY_SEARCH_PART : 63,
- ARRAY_CONTAINS : 64,
- ARRAY_COPY_TO : 65,
- ARRAY_FILL_ALL : 66,
- ARRAY_FILL_PART : 67,
- ARRAY_SORT_ALL : 68,
- ARRAY_SORT_PART : 69,
- LIST_ADD : 70,
- LIST_ADD_RANGE : 71,
- LIST_ALL : 72,
- LIST_ANY : 73,
- LIST_CLEAR : 74,
- LIST_CONTAINS : 75,
- LIST_COPY_TO : 76,
- LIST_COUNT : 77,
- LIST_INDEX_OF : 78,
- LIST_INSERT : 79,
- LIST_LAST : 80,
- LIST_REMOVE_AT : 81,
- LIST_REMOVE_RANGE : 82,
- LIST_SORT_ALL : 83,
- LIST_SORT_PART : 84,
- QUEUE_CLEAR : 85,
- QUEUE_COUNT : 86,
- QUEUE_DEQUEUE : 87,
- QUEUE_ENQUEUE : 88,
- QUEUE_PEEK : 89,
- STACK_CLEAR : 90,
- STACK_COUNT : 91,
- STACK_PEEK : 92,
- STACK_PUSH : 93,
- STACK_POP : 94,
- HASH_SET_ADD : 95,
- HASH_SET_CLEAR : 96,
- HASH_SET_CONTAINS : 97,
- HASH_SET_COUNT : 98,
- HASH_SET_REMOVE : 99,
- SORTED_SET_ADD : 100,
- SORTED_SET_CLEAR : 101,
- SORTED_SET_CONTAINS : 102,
- SORTED_SET_COUNT : 103,
- SORTED_SET_REMOVE : 104,
- DICTIONARY_ADD : 105,
- DICTIONARY_CLEAR : 106,
- DICTIONARY_CONTAINS_KEY : 107,
- DICTIONARY_COUNT : 108,
- DICTIONARY_REMOVE : 109,
- SORTED_DICTIONARY_CLEAR : 110,
- SORTED_DICTIONARY_CONTAINS_KEY : 111,
- SORTED_DICTIONARY_COUNT : 112,
- SORTED_DICTIONARY_REMOVE : 113,
- ORDERED_DICTIONARY_CLEAR : 114,
- ORDERED_DICTIONARY_CONTAINS_KEY : 115,
- ORDERED_DICTIONARY_COUNT : 116,
- ORDERED_DICTIONARY_REMOVE : 117,
- TEXT_WRITER_WRITE : 118,
- TEXT_WRITER_WRITE_CHAR : 119,
- TEXT_WRITER_WRITE_CODE_POINT : 120,
- TEXT_WRITER_WRITE_LINE : 121,
- CONSOLE_WRITE : 122,
- CONSOLE_WRITE_LINE : 123,
- STRING_WRITER_CLEAR : 124,
- STRING_WRITER_TO_STRING : 125,
- CONVERT_TO_BASE64_STRING : 126,
- U_T_F8_GET_BYTE_COUNT : 127,
- U_T_F8_GET_BYTES : 128,
- U_T_F8_GET_STRING : 129,
- ENVIRONMENT_GET_ENVIRONMENT_VARIABLE : 130,
- REGEX_COMPILE : 131,
- REGEX_ESCAPE : 132,
- REGEX_IS_MATCH_STR : 133,
- REGEX_IS_MATCH_REGEX : 134,
- MATCH_FIND_STR : 135,
- MATCH_FIND_REGEX : 136,
- MATCH_GET_CAPTURE : 137,
- MATH_METHOD : 138,
- MATH_ABS : 139,
- MATH_CEILING : 140,
- MATH_CLAMP : 141,
- MATH_FUSED_MULTIPLY_ADD : 142,
- MATH_IS_FINITE : 143,
- MATH_IS_INFINITY : 144,
- MATH_IS_NA_N : 145,
- MATH_LOG2 : 146,
- MATH_MAX_INT : 147,
- MATH_MAX_DOUBLE : 148,
- MATH_MIN_INT : 149,
- MATH_MIN_DOUBLE : 150,
- MATH_ROUND : 151,
- MATH_TRUNCATE : 152
+ JSON_VALUE_KIND_ENUM : 37,
+ JSON_ELEMENT_CLASS : 38,
+ LOCK_CLASS : 39,
+ STRING_LENGTH : 40,
+ ARRAY_LENGTH : 41,
+ CONSOLE_ERROR : 42,
+ MAIN : 43,
+ CLASS_TO_STRING : 44,
+ MATCH_START : 45,
+ MATCH_END : 46,
+ MATCH_LENGTH : 47,
+ MATCH_VALUE : 48,
+ JSON_ELEMENT_VALUE_KIND : 49,
+ MATH_NA_N : 50,
+ MATH_NEGATIVE_INFINITY : 51,
+ MATH_POSITIVE_INFINITY : 52,
+ ENUM_FROM_INT : 53,
+ ENUM_HAS_FLAG : 54,
+ INT_TRY_PARSE : 55,
+ LONG_TRY_PARSE : 56,
+ DOUBLE_TRY_PARSE : 57,
+ STRING_CONTAINS : 58,
+ STRING_ENDS_WITH : 59,
+ STRING_INDEX_OF : 60,
+ STRING_LAST_INDEX_OF : 61,
+ STRING_REPLACE : 62,
+ STRING_STARTS_WITH : 63,
+ STRING_SUBSTRING : 64,
+ ARRAY_BINARY_SEARCH_ALL : 65,
+ ARRAY_BINARY_SEARCH_PART : 66,
+ ARRAY_CONTAINS : 67,
+ ARRAY_COPY_TO : 68,
+ ARRAY_FILL_ALL : 69,
+ ARRAY_FILL_PART : 70,
+ ARRAY_SORT_ALL : 71,
+ ARRAY_SORT_PART : 72,
+ LIST_ADD : 73,
+ LIST_ADD_RANGE : 74,
+ LIST_ALL : 75,
+ LIST_ANY : 76,
+ LIST_CLEAR : 77,
+ LIST_CONTAINS : 78,
+ LIST_COPY_TO : 79,
+ LIST_COUNT : 80,
+ LIST_INDEX_OF : 81,
+ LIST_INSERT : 82,
+ LIST_LAST : 83,
+ LIST_REMOVE_AT : 84,
+ LIST_REMOVE_RANGE : 85,
+ LIST_SORT_ALL : 86,
+ LIST_SORT_PART : 87,
+ QUEUE_CLEAR : 88,
+ QUEUE_COUNT : 89,
+ QUEUE_DEQUEUE : 90,
+ QUEUE_ENQUEUE : 91,
+ QUEUE_PEEK : 92,
+ STACK_CLEAR : 93,
+ STACK_COUNT : 94,
+ STACK_PEEK : 95,
+ STACK_PUSH : 96,
+ STACK_POP : 97,
+ HASH_SET_ADD : 98,
+ HASH_SET_CLEAR : 99,
+ HASH_SET_CONTAINS : 100,
+ HASH_SET_COUNT : 101,
+ HASH_SET_REMOVE : 102,
+ SORTED_SET_ADD : 103,
+ SORTED_SET_CLEAR : 104,
+ SORTED_SET_CONTAINS : 105,
+ SORTED_SET_COUNT : 106,
+ SORTED_SET_REMOVE : 107,
+ DICTIONARY_ADD : 108,
+ DICTIONARY_CLEAR : 109,
+ DICTIONARY_CONTAINS_KEY : 110,
+ DICTIONARY_COUNT : 111,
+ DICTIONARY_REMOVE : 112,
+ SORTED_DICTIONARY_CLEAR : 113,
+ SORTED_DICTIONARY_CONTAINS_KEY : 114,
+ SORTED_DICTIONARY_COUNT : 115,
+ SORTED_DICTIONARY_REMOVE : 116,
+ ORDERED_DICTIONARY_CLEAR : 117,
+ ORDERED_DICTIONARY_CONTAINS_KEY : 118,
+ ORDERED_DICTIONARY_COUNT : 119,
+ ORDERED_DICTIONARY_REMOVE : 120,
+ TEXT_WRITER_WRITE : 121,
+ TEXT_WRITER_WRITE_CHAR : 122,
+ TEXT_WRITER_WRITE_CODE_POINT : 123,
+ TEXT_WRITER_WRITE_LINE : 124,
+ CONSOLE_WRITE : 125,
+ CONSOLE_WRITE_LINE : 126,
+ STRING_WRITER_CLEAR : 127,
+ STRING_WRITER_TO_STRING : 128,
+ CONVERT_TO_BASE64_STRING : 129,
+ U_T_F8_GET_BYTE_COUNT : 130,
+ U_T_F8_GET_BYTES : 131,
+ U_T_F8_GET_STRING : 132,
+ ENVIRONMENT_GET_ENVIRONMENT_VARIABLE : 133,
+ REGEX_COMPILE : 134,
+ REGEX_ESCAPE : 135,
+ REGEX_IS_MATCH_STR : 136,
+ REGEX_IS_MATCH_REGEX : 137,
+ MATCH_FIND_STR : 138,
+ MATCH_FIND_REGEX : 139,
+ MATCH_GET_CAPTURE : 140,
+ JSON_ELEMENT_PARSE : 141,
+ JSON_ELEMENT_TRY_PARSE : 142,
+ JSON_ELEMENT_GET_OBJECT : 143,
+ JSON_ELEMENT_GET_ARRAY : 144,
+ JSON_ELEMENT_GET_STRING : 145,
+ JSON_ELEMENT_GET_DOUBLE : 146,
+ JSON_ELEMENT_GET_BOOLEAN : 147,
+ MATH_METHOD : 148,
+ MATH_ABS : 149,
+ MATH_CEILING : 150,
+ MATH_CLAMP : 151,
+ MATH_FUSED_MULTIPLY_ADD : 152,
+ MATH_IS_FINITE : 153,
+ MATH_IS_INFINITY : 154,
+ MATH_IS_NA_N : 155,
+ MATH_LOG2 : 156,
+ MATH_MAX_INT : 157,
+ MATH_MAX_DOUBLE : 158,
+ MATH_MIN_INT : 159,
+ MATH_MIN_DOUBLE : 160,
+ MATH_ROUND : 161,
+ MATH_TRUNCATE : 162
}
class FuDocInline
@@ -3209,7 +3219,7 @@ export class FuSystem extends FuScope
stackClass.addMethod(this.#typeParam0, FuId.STACK_POP, "Pop", true);
this.#addSet(FuId.HASH_SET_CLASS, "HashSet", FuId.HASH_SET_ADD, FuId.HASH_SET_CLEAR, FuId.HASH_SET_CONTAINS, FuId.HASH_SET_COUNT, FuId.HASH_SET_REMOVE);
this.#addSet(FuId.SORTED_SET_CLASS, "SortedSet", FuId.SORTED_SET_ADD, FuId.SORTED_SET_CLEAR, FuId.SORTED_SET_CONTAINS, FuId.SORTED_SET_COUNT, FuId.SORTED_SET_REMOVE);
- this.#addDictionary(FuId.DICTIONARY_CLASS, "Dictionary", FuId.DICTIONARY_CLEAR, FuId.DICTIONARY_CONTAINS_KEY, FuId.DICTIONARY_COUNT, FuId.DICTIONARY_REMOVE);
+ let dictionaryClass = this.#addDictionary(FuId.DICTIONARY_CLASS, "Dictionary", FuId.DICTIONARY_CLEAR, FuId.DICTIONARY_CONTAINS_KEY, FuId.DICTIONARY_COUNT, FuId.DICTIONARY_REMOVE);
this.#addDictionary(FuId.SORTED_DICTIONARY_CLASS, "SortedDictionary", FuId.SORTED_DICTIONARY_CLEAR, FuId.SORTED_DICTIONARY_CONTAINS_KEY, FuId.SORTED_DICTIONARY_COUNT, FuId.SORTED_DICTIONARY_REMOVE);
this.#addDictionary(FuId.ORDERED_DICTIONARY_CLASS, "OrderedDictionary", FuId.ORDERED_DICTIONARY_CLEAR, FuId.ORDERED_DICTIONARY_CONTAINS_KEY, FuId.ORDERED_DICTIONARY_COUNT, FuId.ORDERED_DICTIONARY_REMOVE);
let textWriterClass = FuClass.new(FuCallType.NORMAL, FuId.TEXT_WRITER_CLASS, "TextWriter");
@@ -3264,6 +3274,29 @@ export class FuSystem extends FuScope
matchClass.add(FuProperty.new(this.#uIntType, FuId.MATCH_LENGTH, "Length"));
matchClass.add(FuProperty.new(this.stringStorageType, FuId.MATCH_VALUE, "Value"));
this.add(matchClass);
+ let jsonValueKindEnum = this.newEnum(false);
+ jsonValueKindEnum.isPublic = true;
+ jsonValueKindEnum.id = FuId.JSON_VALUE_KIND_ENUM;
+ jsonValueKindEnum.name = "JsonValueKind";
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "Object", visitStatus: FuVisitStatus.DONE }));
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "Array", visitStatus: FuVisitStatus.DONE }));
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "String", visitStatus: FuVisitStatus.DONE }));
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "Number", visitStatus: FuVisitStatus.DONE }));
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "True", visitStatus: FuVisitStatus.DONE }));
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "False", visitStatus: FuVisitStatus.DONE }));
+ FuSystem.#addEnumValue(jsonValueKindEnum, Object.assign(new FuConst(), { visibility: FuVisibility.PUBLIC, name: "Null", visitStatus: FuVisitStatus.DONE }));
+ this.add(jsonValueKindEnum);
+ let jsonElementClass = FuClass.new(FuCallType.SEALED, FuId.JSON_ELEMENT_CLASS, "JsonElement");
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, this.voidType, FuId.JSON_ELEMENT_PARSE, "Parse", true, FuVar.new(this.stringPtrType, "value")));
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, this.boolType, FuId.JSON_ELEMENT_TRY_PARSE, "TryParse", true, FuVar.new(this.stringPtrType, "value")));
+ let jsonElementPtr = Object.assign(new FuDynamicPtrType(), { class: jsonElementClass });
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, Object.assign(new FuClassType(), { class: dictionaryClass, typeArg0: this.stringStorageType, typeArg1: jsonElementPtr }), FuId.JSON_ELEMENT_GET_OBJECT, "GetObject", false));
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, Object.assign(new FuClassType(), { class: listClass, typeArg0: jsonElementPtr }), FuId.JSON_ELEMENT_GET_ARRAY, "GetArray", false));
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, this.stringPtrType, FuId.JSON_ELEMENT_GET_STRING, "GetString", false));
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, this.doubleType, FuId.JSON_ELEMENT_GET_DOUBLE, "GetDouble", false));
+ jsonElementClass.add(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.NORMAL, this.boolType, FuId.JSON_ELEMENT_GET_BOOLEAN, "GetBoolean", false));
+ jsonElementClass.add(FuProperty.new(jsonValueKindEnum, FuId.JSON_ELEMENT_VALUE_KIND, "ValueKind"));
+ this.add(jsonElementClass);
let floatIntType = Object.assign(new FuFloatingType(), { id: FuId.FLOAT_INT_TYPE, name: "float" });
let mathClass = FuClass.new(FuCallType.STATIC, FuId.NONE, "Math");
mathClass.add(FuMethodGroup.new(FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.STATIC, this.intType, FuId.MATH_ABS, "Abs", false, FuVar.new(this.longType, "a")), FuMethod.new(null, FuVisibility.PUBLIC, FuCallType.STATIC, this.#floatType, FuId.MATH_ABS, "Abs", false, FuVar.new(this.doubleType, "a"))));
@@ -3389,6 +3422,7 @@ export class FuSystem extends FuScope
dict.add(FuMethod.new(dict, FuVisibility.FINAL_VALUE_TYPE, FuCallType.NORMAL, this.voidType, FuId.DICTIONARY_ADD, "Add", true, FuVar.new(this.#typeParam0, "key")));
dict.addMethod(this.boolType, containsKeyId, "ContainsKey", false, FuVar.new(this.#typeParam0, "key"));
dict.addMethod(this.voidType, removeId, "Remove", true, FuVar.new(this.#typeParam0, "key"));
+ return dict;
}
static #addEnumValue(enu, value)
@@ -15937,6 +15971,10 @@ export class GenCs extends GenTyped
this.include("System.Text.RegularExpressions");
this.write(klass.class.name);
break;
+ case FuId.JSON_ELEMENT_CLASS:
+ this.include("System.Text.Json");
+ this.write("JsonElement");
+ break;
case FuId.LOCK_CLASS:
this.write("object");
break;
@@ -16340,6 +16378,20 @@ export class GenCs extends GenTyped
args[0].accept(this, FuPriority.ARGUMENT);
this.write("].Value");
break;
+ case FuId.JSON_ELEMENT_PARSE:
+ obj.accept(this, FuPriority.ASSIGN);
+ this.write(" = JsonDocument.Parse(");
+ args[0].accept(this, FuPriority.ARGUMENT);
+ this.write(").RootElement");
+ break;
+ case FuId.JSON_ELEMENT_GET_OBJECT:
+ this.include("System.Linq");
+ this.writePostfix(obj, ".EnumerateObject().ToDictionary(p => p.Name, p => p.Value)");
+ break;
+ case FuId.JSON_ELEMENT_GET_ARRAY:
+ this.include("System.Linq");
+ this.writePostfix(obj, ".EnumerateArray().ToList()");
+ break;
case FuId.MATH_METHOD:
case FuId.MATH_ABS:
case FuId.MATH_CEILING:
diff --git a/test/JsonElement.fu b/test/JsonElement.fu
new file mode 100644
index 0000000..f622b9c
--- /dev/null
+++ b/test/JsonElement.fu
@@ -0,0 +1,38 @@
+public static class Test
+{
+ public static bool Run()
+ {
+ JsonElement() json; //FAIL: c cpp d java js py swift ts TODO; cl
+ json.Parse("\"foo\"");
+ if (json.ValueKind != JsonValueKind.String || json.GetString() != "foo")
+ return false;
+ json.Parse("10.5");
+ if (json.ValueKind != JsonValueKind.Number || json.GetDouble() != 10.5)
+ return false;
+ json.Parse("true");
+ if (json.ValueKind != JsonValueKind.True || !json.GetBoolean())
+ return false;
+ json.Parse("false");
+ if (json.ValueKind != JsonValueKind.False || json.GetBoolean())
+ return false;
+ json.Parse("null");
+ if (json.ValueKind != JsonValueKind.Null)
+ return false;
+
+ json.Parse("[ 5, true ]");
+ if (json.ValueKind != JsonValueKind.Array)
+ return false;
+ List<JsonElement#> list = json.GetArray();
+ if (list.Count != 2 || list[0].GetDouble() != 5 || list[1].ValueKind != JsonValueKind.True)
+ return false;
+
+ json.Parse("{ \"foo\": null, \"bar\": 42 }");
+ if (json.ValueKind != JsonValueKind.Object)
+ return false;
+ Dictionary<string(), JsonElement#> dict = json.GetObject();
+ if (dict.Count != 2 || dict["foo"].ValueKind != JsonValueKind.Null || dict["bar"].GetDouble() != 42)
+ return false;
+
+ return true;
+ }
+}