mirror of
https://onedev.site.tesses.net/crosslang
synced 2026-02-08 17:15:45 +00:00
Add Json Doc
This commit is contained in:
@@ -33,6 +33,8 @@ int main(int argc, char** argv)
|
||||
port = std::stoi(item.second);
|
||||
}
|
||||
}
|
||||
|
||||
env->EnsureDictionary(&gc,"Net")->SetValue("WebServerPort", (int64_t)port);
|
||||
TList* args2 = TList::Create(ls);
|
||||
args2->Add(exePath.ToString());
|
||||
for(auto& item : args.positional)
|
||||
@@ -40,7 +42,7 @@ int main(int argc, char** argv)
|
||||
args2->Add(item);
|
||||
}
|
||||
|
||||
auto res = env->CallFunction(ls, "WebAppMain", {args2});
|
||||
auto res = env->CallFunctionWithFatalError(ls, "WebAppMain", {args2});
|
||||
auto svr2 = Tesses::CrossLang::ToHttpServer(&gc,res);
|
||||
if(svr2 == nullptr) return 1;
|
||||
Tesses::Framework::Http::HttpServer svr(port,svr2);
|
||||
@@ -63,7 +65,7 @@ int main(int argc, char** argv)
|
||||
args->Add(exePath.ToString());
|
||||
for(int arg=1;arg<argc;arg++)
|
||||
args->Add(argv[arg]);
|
||||
auto res = env->CallFunction(ls,"main",{args});
|
||||
auto res = env->CallFunctionWithFatalError(ls,"main",{args});
|
||||
int64_t iresult;
|
||||
if(GetObject(res,iresult))
|
||||
return (int)iresult;
|
||||
|
||||
@@ -39,6 +39,8 @@ int main(int argc, char** argv)
|
||||
port = std::stoi(item.second);
|
||||
}
|
||||
}
|
||||
|
||||
env->EnsureDictionary(&gc,"Net")->SetValue("WebServerPort", (int64_t)port);
|
||||
TList* args2 = TList::Create(ls);
|
||||
args2->Add(exePath.ToString());
|
||||
for(auto& item : args.positional)
|
||||
@@ -46,7 +48,7 @@ int main(int argc, char** argv)
|
||||
args2->Add(item);
|
||||
}
|
||||
|
||||
auto res = env->CallFunction(ls, "WebAppMain", {args2});
|
||||
auto res = env->CallFunctionWithFatalError(ls, "WebAppMain", {args2});
|
||||
auto svr2 = Tesses::CrossLang::ToHttpServer(&gc,res);
|
||||
if(svr2 == nullptr) return 1;
|
||||
Tesses::Framework::Http::HttpServer svr(port,svr2);
|
||||
@@ -69,7 +71,7 @@ int main(int argc, char** argv)
|
||||
args->Add(exePath.ToString());
|
||||
for(int arg=0;arg<argc;arg++)
|
||||
args->Add(argv[arg]);
|
||||
auto res = env->CallFunction(ls,"main",{args});
|
||||
auto res = env->CallFunctionWithFatalError(ls,"main",{args});
|
||||
int64_t iresult;
|
||||
if(GetObject(res,iresult))
|
||||
return (int)iresult;
|
||||
|
||||
@@ -3,7 +3,7 @@ What is TObject
|
||||
|
||||
It is a std::variant that can be (not in that order)
|
||||
|
||||
- Undefined (Tesses::CrossLang::Undefined)
|
||||
- Undefined (std::monostate)
|
||||
- Null (std::nullptr_t)
|
||||
- Long (int64_t)
|
||||
- Double (double)
|
||||
|
||||
@@ -2540,6 +2540,8 @@ class GC {
|
||||
void LoadPlugin(GC* gc, TRootEnvironment* env, Tesses::Framework::Filesystem::VFSPath sharedObjectPath);
|
||||
std::string Json_Encode(TObject o,bool indent=false);
|
||||
TObject Json_Decode(GCList ls,std::string str);
|
||||
std::string Json_DocEncode(TObject o,bool indent);
|
||||
TObject Json_DocDecode(GCList ls,std::string str);
|
||||
//DO NOT USE DIRECTLY
|
||||
class SharedPtrTObject {
|
||||
GCList* ls;
|
||||
|
||||
@@ -58,7 +58,11 @@ int main(int argc, char** argv)
|
||||
|
||||
|
||||
auto p = Tesses::Framework::Platform::Environment::GetRealExecutablePath(Tesses::Framework::Filesystem::LocalFS->SystemToVFSPath(argv[0])).GetParent().GetParent() / "share" / "Tesses" / "CrossLang" / "Tesses.CrossLang.ShellPackage-1.0.0.0-prod.crvm";
|
||||
|
||||
if(argc == 2 && strcmp(argv[1],"configdir") == 0)
|
||||
{
|
||||
std::cout << dir.ToString() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
if(argc > 1 && strcmp(argv[1],"update-shell") == 0)
|
||||
{
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ int main(int argc, char** argv)
|
||||
port = std::stoi(item.second);
|
||||
}
|
||||
}
|
||||
|
||||
env->EnsureDictionary(&gc,"Net")->SetValue("WebServerPort", (int64_t)port);
|
||||
TList* args2 = TList::Create(ls);
|
||||
for(auto& item : args.positional)
|
||||
{
|
||||
|
||||
@@ -81,6 +81,21 @@ namespace Tesses::CrossLang
|
||||
}
|
||||
return "null";
|
||||
}
|
||||
static TObject JsonDocEncode(GCList& ls2, std::vector<TObject> args)
|
||||
{
|
||||
if(args.size() >= 1)
|
||||
{
|
||||
bool indent = (args.size() == 2 && std::holds_alternative<bool>(args[1]) && std::get<bool>(args[1]));
|
||||
|
||||
auto json = JsonSerialize(args[0]);
|
||||
JArray ar;
|
||||
if(TryGetJToken(json,ar))
|
||||
return Json::DocEncode(ar,indent);
|
||||
|
||||
|
||||
}
|
||||
return "";
|
||||
}
|
||||
static TObject JsonDeserialize(GCList& ls2,JToken json)
|
||||
{
|
||||
if(std::holds_alternative<JUndefined>(json)) return nullptr;
|
||||
@@ -138,6 +153,19 @@ namespace Tesses::CrossLang
|
||||
}
|
||||
return Undefined();
|
||||
}
|
||||
static TObject JsonDocDecode(GCList& ls2,std::vector<TObject> args)
|
||||
{
|
||||
if(args.size() > 0 && std::holds_alternative<std::string>(args[0]))
|
||||
{
|
||||
|
||||
|
||||
return JsonDeserialize(ls2, Json::DocDecode(std::get<std::string>(args[0])));
|
||||
|
||||
|
||||
}
|
||||
|
||||
return Undefined();
|
||||
}
|
||||
std::string Json_Encode(TObject o,bool indent)
|
||||
{
|
||||
return Json::Encode(JsonSerialize(o),indent);
|
||||
@@ -146,14 +174,29 @@ namespace Tesses::CrossLang
|
||||
{
|
||||
return JsonDeserialize(ls,Json::Decode(str));
|
||||
}
|
||||
std::string Json_DocEncode(TObject o,bool indent)
|
||||
{
|
||||
auto obj = JsonSerialize(o);
|
||||
JArray ls;
|
||||
if(TryGetJToken(obj,ls))
|
||||
return Json::DocEncode(ls,indent);
|
||||
return "";
|
||||
}
|
||||
TObject Json_DocDecode(GCList ls,std::string str)
|
||||
{
|
||||
return JsonDeserialize(ls,Json::DocDecode(str));
|
||||
}
|
||||
void TStd::RegisterJson(GC* gc,TRootEnvironment* env)
|
||||
{
|
||||
|
||||
env->permissions.canRegisterJSON=true;
|
||||
GCList ls(gc);
|
||||
TDictionary* dict = TDictionary::Create(ls);
|
||||
dict->DeclareFunction(gc, "Decode","Deserialize Json",{"Json string"},JsonDecode);
|
||||
dict->DeclareFunction(gc, "Decode","Deserialize Json",{"jsonString"},JsonDecode);
|
||||
dict->DeclareFunction(gc, "Encode","Serialize Json",{"any","$indent"},JsonEncode);
|
||||
dict->DeclareFunction(gc, "DocDecode", "Deserialize JsonDoc", {"jsonDocString"},JsonDocDecode);
|
||||
dict->DeclareFunction(gc, "DocEncode", "Serialize JsonDoc", {"ls","$indent"},JsonDocEncode);
|
||||
|
||||
|
||||
|
||||
gc->BarrierBegin();
|
||||
|
||||
Reference in New Issue
Block a user