objeck / objeck-lang

Objeck is a modern object-oriented programming language with functional features tailored for machine learning. It emphasizes expression, simplicity, portability, and scalability. The programming environment consists of a compiler, virtual machine, REPL shell, and command line debugger with IDE plugins.
https://objeck.org
Other
154 stars 11 forks source link

Serializing/Deserializing Objeck object to/from JSON #456

Closed ghost closed 6 months ago

ghost commented 7 months ago

The idea is to be able to send/receive Objeck objects between processes, over the network,...

objeck commented 7 months ago

Just data?

ghost commented 7 months ago

Just data?

I think so.

objeck commented 7 months ago

JSON elements would have to map to the existing Collection classes.

ghost commented 7 months ago

JSON elements would have to map to the existing Collection classes.

https://github.com/objeck/objeck-lang/issues/421 https://github.com/objeck/objeck-lang/issues/422

ghost commented 7 months ago

https://github.com/objeck/objeck-lang/issues/384 https://github.com/objeck/objeck-lang/issues/385 https://github.com/objeck/objeck-lang/issues/398

objeck commented 7 months ago

For clarity, the code must use an existing JSON parser and return associated collections.

Mapping:

All of the above types are supported in the JsonElement class, so the ask is really to map the JsonElement to the internal collections it uses.

objeck commented 7 months ago

I will defer this; it seems like a "nice to have," which I do have time to implement. Please, refer to the prior comment.

objeck commented 7 months ago

After looking into this more...

What about type-checking? Generate an exception if the wrong data type is cast?

The existing JsonElement class supports a type and a value. In dynamically typed languages, a value is a type/value. However, a value must map to a type in statically typed languages, which requires runtime type checking.

What is your proposed solution?

ghost commented 7 months ago

After looking into this more...

What about type-checking? Generate an exception if the wrong data type is cast?

The existing JsonElement class supports a type and a value. In dynamically typed languages, a value is a type/value. However, a value must map to a type in statically typed languages, which requires runtime type checking.

What is your proposed solution?

Does Objeck support Reflection? What does System.Introspection do?

Does Objeck support something equivalent?

https://www.geeksforgeeks.org/typeof-operator-keyword-in-c-sharp/

Btw, yes, this is the advantage of a language that supports exceptions. Everything is so much easier with exceptions.

https://code-maze.com/csharp-how-to-ensure-a-string-is-valid-json/

ghost commented 6 months ago

I know you will never add support for exceptions, so I will close this.

objeck commented 6 months ago

Objeck supports reflection via the System.Introspection class; however, that is not tied to supporting exceptions. In my professional experience, most developers either log or throw exceptions versus writing proper error-handling routines.

objeck commented 6 months ago

Please refer to the code below. I don't think parsing JSON into Collection classes is beneficial in Objeck since it is a strongly typed language. The cast statements are distracting.

With Collections

root := JsonParser->New(document)<Base>;
if(root <> Nil) {
    success := root->As(Map)->Find("success");
    if(success <> Nil) {
        success->As(String)->Equals("true")->PrintLine();
    };
};

With JsonElement

parser := JsonParser->New(document);
if(parser->Parse()) {
    root := parser->GetRoot();
    success := root->Get("success");
    if(success <> Nil) {
        success->GetString()->Equals("true")->PrintLine();
    };
};