Serialize Flex Objects to Save/Restore Application State - actionscript

Is it possible to serialize a hierarchy of objects in Flex, send the binary data to a URL for storage/retrieval on/from a server, and deserialize the data to restore the objects' original state?
I know it's possible to convert the objects into an XML format (haven't tried it yet), but I'm hoping to avoid parsing XML and rebuilding the objects manually. It would be nice to have functionality which can serialize/deserialize objects to a simple binary format (I did something similar in the past in Java, though not quite as easily as I would have liked). The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.
In pseudo-code, here's what I would like to do:
private var contentToSave:HBox = new HBox();
private function saveState(event:Event):void {
var toSave:HBox = this.contentToSave;
var data:? = /* serialize 'toSave' ActionScript classes to binary data*/;
sendDataToServer(data, filename);
}
private function restoreState(filename):void {
var data:? = getDataFromServer(filename);
var savedData:HBox = /* deserialize binary 'data' to ActionScript classes */;
this.contentToSave = savedData;
}

Take a look at ByteArray.writeObject(). which saves the passed object in AMF format into the byte array. I have not used this function too much, I don't exactly know what kind of objects it can serialize, but definitely not all.

Try the JSON based serialization package in ascorelib.
[...]but I'm hoping to avoid parsing XML and rebuilding the objects manually
AS handles XML just like any other native type. Rest assured. XML is the preferred way of dealing with data you will be pulling off and putting back on a server. Of course, the ascorelib gives you a JSON class -- so you may want to look at that as well.
The 'eval' function in Perl is similar to what I'm looking for, sans saving code, of course.
IIRC, eval is part of the ECMAScript specification (and you will find it in Javascript). But not in AS3.0. It was there to a certain extent in some previous version(s?) but is no longer supported.

Related

How to use FSharp.Data.JsonProvider dynamically?

I'm parsing JSON file using FSharp.Data.JsonProvider.
From http://fsharp.github.io/FSharp.Data/library/JsonProvider.html,
I have to declare type like this.
type Simple = JsonProvider<"path/to/simple/json.json">
But I want make the JsonProvider more flexible.
What I mean by flexible is to declare like below.
let x = "path/to/simple/json.json"
type Simple = JsonProvider<x>
So, the JsonProvider can deal with various json files, rather than only specific ones.
How can I do this? Or, there are other approaches?
If your files have the same shape (ie same fields and field types), then you can use one as the type parameter, and then pass the others to Parse:
open System.IO
type Simple = JsonProvider<"file1.json">
let value1 = Simple.GetSample()
let value2 = Simple.Parse(File.ReadAllText "file2.json")
Edit: what if they don't have the same shape? Well, there are two cases.
If they have minor differences, like some fields that may be present or absent, or may be eg a string or a number, then you can make a sample file that is an array of samples and use JsonProvider<"sample.json", SampleIsList = true>. The generated type will accommodate for the differences using optional values.
If they are completely different, then JsonProvider simply isn't the right tool for the job. The purpose of JsonProvider is to give a nice API to read JSON when you know in advance what shape it's going to have. If you don't, then you should instead use a generic JSON parser with a document type, like System.Text.Json's JsonDocument or Newtonsoft.Json's JObject. I guess if you really want to keep using JsonProvider, you could also use the document type used by JsonProvider behind the scenes (called JsonValue).

How to return a struct from an imported DLL-function in MQL4?

Is there a way to return a struct from an imported function in MQL4, without having to pass it as a parameter and making a memcpy?
Be cautious with any kind of DLL-interfacing, MQL4 Documentation states:
Passing ParametersAll parameters of simple types are passed by values unless it is explicitly indicated that they are passed by reference. When a string is passed, the address of the buffer of the copied string is passed; if a string is passed by reference, the address of the buffer of this string without copying it is passed to the function imported from DLL.Structures that contain dynamic arrays[], strings, classes, other complex structures, as well as static or dynamic arrays[] of the enumerated objects, can't be passed as a parameter to an imported function.When passing an array to DLL, the address of the beginning of the data buffer is always passed (irrespective of the AS_SERIES flag). A function inside a DLL knows nothing about the AS_SERIES flag, the passed array is a static array of an undefined length; an additional parameter should be used for specifying the array size.
More glitches apply... Then how to make it work?
Maybe a straight, heterogeneous multi-party distributed processing, which communicates rather results than function calls, independent of all nightmares of maintaining just DLL-imported functions API changes, is a way safer way to go. Using this approach for the last few years and since than have no problems with New-MQL4.56789 string-s that seized to remain string-s and silently started to become struct-s etc.
Worth to know about.
Anyway, welcome and enjoy the Wild Worlds of MQL4 -- may enjoy to click and read other posts on issues in MQL4/DLL integration and/or signalling/messaging in MQL4 domains. Feel free to ask more

How to define content transformers in Siesta?

I'm just integrating Siesta and I love it, it solves a lot of issues we have when using frameworks like RestKit.
What I can't get my head around is how to use the content transformers? I've looked at the docs and examples and I can't quite understand how it works, I'm also fairly new to Swift.
Looking at this example taken from another SO reply:
private let SwiftyJSONTransformer = ResponseContentTransformer(skipWhenEntityMatchesOutputType: false) {
JSON($0.content as AnyObject)
}
I can't quite understand what's going on here, there is no return value so I don't understand how content is being transformed. This might be my due to a lack of deep Swift knowledge.
I've understand how NSValueTransformer objects work in Obj-C but I can't work out how to map a response abit JSON or just a simple response body like a single string, number of boolean value to a object or type using Siesta.
We have some API responses that return just a single BOOL value in the response body while most of the other API responses are complex JSON object graphs.
How would I go about mapping these responses to more primitive types and or more complex objects.
Thanks.
Some of your confusion is basic Swift stuff. Where a closure uses $0 and contains only a single statement, the input types are inferred and the return is implicit. Thus the code in your question is equivalent to:
ResponseContentTransformer(skipWhenEntityMatchesOutputType: false) {
(content: AnyObject, entity: Entity) in
return JSON(content)
}
(Using $0.content instead of just $0 is a workaround for a maybe-bug-maybe-feature in Swift where $0 becomes a tuple of all arguments instead of just the first one. Don’t worry too much about it; $0.content is just a magic incantation you can use in your Siesta transformers.)
The other half of your confusion is Siesta itself. The general approach is as follows:
Configure a generic transformer that turns the raw NSData into a decoded but unstructured type such as String or Dictionary.
You’ll usually configure this based on content type.
Siesta includes parsing for strings, JSON, and images unless you turn it off with useDefaultTransformers: false.
Optionally configure a second transformer that turns the unstructured type into a model.
You’ll usually configure this based on API path.
Siesta doesn’t include any of this by default; it’s all per app.
For responses that are just a bare boolean, you’d probably do only #1 — depending on exactly what kind of response the server is sending, and depending on how you know it's just a boolean.
I recommend looking at the example project included with Siesta, which gives a good example of how all this plays out. You’ll see examples of both transformers that conditionally operate on the content based on its type (#1) and model-specific tranformers (#2) in that code.

Should I use NSCoder to serialise to JSON?

There are a number of questions and answers on SO that ask how do I serialise an object to in objective c.
Serialize and Deserialize Objective-C objects into JSON
Objective C serialize list of complex objects
Serialize custom object to JSON which contains NSMutableArray
How to serialize a class in IOS sdk (Objective-c)?
The following 3 methods are all mentioned in the above links.
1) Use NSJSONSerialization to serialise the object to JSON. Seems good but this requires the object in question to be either an array or dictionary at its top level. Common solution is to declare custom toDictionary or serialise method that loops over the properties and sets the relevant key and values.
2) Conform to the NSCoder protocol, a little like the approach above but there seems to be some confusion around whether or not this can serialise to JSON or just to NSData.
3) Third party library.
I'm getting slightly confused as to what approach to take. I want to serialise to JSON, there are contradictory answers some stating you can use NSCoder some saying not. I know that a third party app will work however I'd rather implement something simple like options 1 or 2.
Thoughts?
With 1, you'd basically be writing a JSON-based implementation of NSCoder from scratch. Certainly doable, though.
With 2, I believe it might be possible, since I think the output of NSCoder is some variant of XML (though compressed into a binary blob). However, I don't know if this is a great approach, since the format is proprietary and not really meant to be human-editable. There might also be a mismatch between what's allowed in JSON vs. the NSCoder format in terms of keys and leaf nodes, forcing you to do a messy conversion.
I've been trying to do something similar, and based on my research, I actually suggest 3. Using something like Mantle — a stable, polished framework that gets frequent updates — you can specify exactly how your model objects will be serialized to and deserialized from JSON. It even supports the NSCoder protocol as an option! (This is effectively solution 1, but vetted and maintained by a 3rd party.)

How to parse JSON string without using any API?

I need to use the JSON parser in my iPhone application. We have API's which are used to parse the data.
I just want to know, how can we do without using any API ?
Thank you.
Use the APIs.
Use the APIs.
Use the APIs.
They're quick, they've been tested to work, and you don't have to think about them.
If you're still committed to writing a JSON parser (and/or if this is an academic pursuit), then you're likely to benefit from researching the JSON specification and brushing up on [your platform of choice]'s string operations and regular expressions library. Then, as #alexanderb suggests, create a small library of classes and/or functions to support you.
Yes, you can. Since JSON is a simple string, you could create your own parser that is able to serialize string to target object.
For instance, you have JSON like that
{ description: "my description", value: 10 }
You can extract description value, by regex and return object of class, like
class Target
{
string Description;
int value;
}
But, of cause it always a good idea to use time-proven JSON parsers.
Copy the 8k of these 238 lines (+/- comments) into your code:
http://code.google.com/p/json-sans-eval/source/browse/trunk/src/json_sans_eval.js?r=12

Resources