I want to use the serialization framework of Einhugur (http://www.einhugur.com/Html/SerializationFramework/index.html) to transmit classes over sockets. But, when I receive the JSON file at the server side, I want to transform the JSON back in a class. The main problem is that I don’t know what class I received.
I can check the name of the class in JSON, and then create the right class, but that would result in an IF statement for every class I have. I tought it maybe could be done by introspection, but I don’t know how. Any tips on an approach? Maybe with XojoScript?
Create a global dictionary (for example in a module).
Register all possible classes in the dictionary (for example in App.Open):
dct.Value("ClassA") = GetTypeInfo(ClassA)
dct.Value("ClassB") = GetTypeInfo(ClassB)
...
When you receive the JSON create an instance of the class via introspection:
Dim ti As Introspection.TypeInfo = dct.Value("ClassA")
Dim cis() As Introspection.ConstructorInfo = ti.GetConstructors()
Dim ci As Introspection.ConstructorInfo = cis(0)
Dim myInstance As Object = ci.Invoke()
[quote=191012:@Norman Palardy]This is the fancy version of an IF or SELECT CASE statement
In the end there is no “class by name” like there is in something like Java[/quote]
I don’t see how this is an fancy IF or SELECT statement. This picks the right classname out of a dictionary (or a array) and instances that class. Adding classes is simply done by adding them to the collection of classes. With an if statement this would mean copy pasting elseif statements and would result in a bunch of redundant code, isn’t it?
You need to list all classes, which is in the end the same work as having an IF or SELECT CASE statement.
Internally a dictionary is (usually) based on hashing, which can be seen as a very clever IF or SELECT CASE.
Isn’t in the end any program just a whole lot of complex, nested IFs? I once read about the “One instruction set computer”. You need branching or you won’t be able to program anything.
Still amounts to a handy way to list every possible class you can create which is no better / worse than a long if then else or select case listing them all
In Java’s case the runtime support “ClassByName” without having to register anything and you can construct it simply by having the name
With such a change, it would no longer be possible to strip unused classes from a build, because the compiler could not know which classes were unused.
Add a build option to not strip unused classes for people who want this feature. And/or a per class option for inclusion. Either way if the TypeInfo isn’t found by name return nil.
you can actually already control if a class gets stripped or not
I think, for now, a declaration that uses it will do
dim t as classNotToStrip
Not sure if this will continue to work or not with LLVM as its much better about figuring out what code can be stripped and whats unused etc
Assuming the code is reachable and not itself stripped out, there’s two guaranteed ways to make a class not be entirely stripped: New and GetTypeInfo. Even this is a bit of an implementation detail because there’s no way in the program to observe whether or not it’s been stripped out.
It’d only be a minor convenience to have a GetTypeInfo(“name”) built in, along with IDE control over including classes.
Making your own GetTypeInfo(“name”) and maintaining a master method where all the lookupable classes are listed isn’t that much trouble.
Although, private classes in a module can’t easily be added. Listing spreads out, there’s no longer a single master method to maintain and it gets clumsy.