how to serialize a cfc instance - coldfusion

The <cfwddx> tag can easily be used to serialize and deserialize complex data types such as arrays, structs and queries, but not cfc instances.
What's the best way to serialize and later deserialize a cfc?

Because of the nature of a CFC instance (data hiding), the wddx process cannot look "inside" of it in order to serialize it.
However, you do have two options for CFC instance serialization, depending on what you're looking to accomplish.
If you want to serialize the internal state of your CFC instance, you should just add a method within your CFC which returns the variables scope of the CFC instance. You can then simply serialize the response from calling that method. getMemento() or getInstanceData() should work. -- Thanks Scott Stroz!
If you want to serialize the metadata (properties and methods) of the CFC instance, you could use ColdFusion's build in function getComponentMetadata() and serialize the result of that call. -- Thanks Dan Bracuk!

Check out ObjectSave() and ObjectLoad(), but IMO they can be risky if the serialized data is to be loaded in a different version of CF down the road.

Related

Reading/Writing EDN/Map into a cookie

I'm writing a simple SPA game in ClojureScript and I want to save the game state, which is a map, to a cookie and read it back.
I'm successfully saving the cookie, though it turns my map to a string.
When I read it back, I get Error: No protocol method IAssociative.-assoc defined for type string:(then it outputs the map)
What's the simplest and best way to use maps with cookies?
I have tried https://github.com/reagent-project/reagent-utils and https://github.com/Quantisan/cljs-cookies, but I get similar issues.
I think you're looking for "cljs.core/read-string cljs.core/pr-str" functions. You need to serialize your Clojure objects to string and read it back to store it in a cookie. Functions above will be able to serialize all standard Clojure(Script) data structures for you. And then if you have something custom, this function can come to rescue: https://cljs.github.io/api/cljs.reader/register-tag-parserBANG.

DRF - Serialize and Deserialize further explanation

I am starting to work with Django Rest Framework and I am a little confused with Serializers, sometimes its called to serialize and sometimes to deserialize, sometimes is called with the data param, and sometimes it doesn't. When and how is a good use for serializers to serialize and deserialize?
Serialization is the process of preparing your data to be sent ower the network in the case of REST. The result of the serialization is a json/xml in the case of Django REST framework. So you need to serialize your data when you get it, and you deserialize it when you save it to the model with a POST/PUT request.
For further reading: http://www.django-rest-framework.org/api-guide/serializers/
Serialization: Converting complex data types like queryset to native data types like dictionary.
DeSerialization: Converting python native data types to Complex data types.
so both the actions are accomplished with the same serializer. basically when we call a serializer with parameter data (like serializer=Serializer(data=data_dict), it return a complex object. and when we call it with complex object, it gives a dictionary.
So basically there is nothing like deserializer. Serializers perform both serialization and deserialization depending on the parameter we pass and that's how we call it.
we serializer data to send over the network so that it can be easily interpreted and rendered easily in the browser. we deserialize data to get complex object and to probably save it to database.
Thanks! Hope you find this helpful.:)

How to pass parameters to scheduled task via cfschedule?

Is there any way to pass parameters or share data with a scheduled task? I understand that you can pass serializable arguments to a Quartz Job, but this seems not to be available in cfschedule. What are the options to achieve this?
The easiest way to do that is just to have a .cfm file that is called by cfschedule that itself calls the CFC and passes the desired methods.
If you want a more flexible solution, I have a Scheduler.cfc that allows you to have a method called at an frequency that you want and you have even pass arguments for the method call.
http://www.bryantwebconsulting.com/blog/index.cfm/2009/2/26/Schedulercfc-10
It can be gotten here.
https://github.com/sebtools/com.sebtools/
The important thing with it is that you have to have Scheduler instantiated into Application scope and a .cfm that is called by cfschedule that runs:
If you just have one method with arguments that needs to be called frequently, then Scheduler.cfc is overkill over the simple solution, but if this is a general problem that you need to solve more frequently, then it can pay off nicely.
You could pass them on the query string of the URL attribute.
example.com/index.cfm?param1=value1&param2=param2
If your data is complex you can always serialize it to JSON before and use deserializeJSON on the receiving task.

What is the difference between different normalization/serialization hooks in ember data?

There are many hooks in ember data v1.0.0-beta.16.1.
Its just hard to understand the purpose of these hooks. To a novice it seems all of them are meant to do the same thing.
I am looking to understand applications of,
A. normalize and normalizeHash
B. serialize
C. normalizePayload
The docs explain them pretty well, but here is my take on it:
normalize is used to modify root object in incoming payload.
normalizeHash is used to modify individual object properties in incoming payload.
serialize is used to modify the outgoing object being sent to server.
normalizedPayload is used to remove extraneous data from incoming payload such as meta data.

Why do we need to create an instance of a CFC?

I'm using the CreateObject() method to create an instance of a CFC and then interacting with this newly created 'instance'. I'm doing this because that's how it seems to be done, but I don't understand why we do this.
Why can't we just interact with the CFC directly instead of creating an instance of it?
A CFC is just a file with some code in it, so it makes little sense to suggest "interacting" with it, just the same as you might suggest "interacting" with a CFM file without <cfinclude>-ing it or similar.
A CFC defines a component, and to use a component, one creates an instance of it. In some languages - eg Java - one can have static properties and methods, and one can access them via the class rather than necessarily object, but CFML does not have this concept. CFCs define components which are used as objects, just the same as in other languages a class defines what it is to be an object, and to use an object, one first needs to create an instance of it.
You can call the cfc directly using cfinvoke. You just have to realize that cfinvoke creates an object of the cfc first, then executes the method you invoked. Also, once the method is invoked, the object is no longer available.
If your .cfm page is only going to use one method of the component, cfinvoke is ok because there is less code for you to write. However, if you use two or more, it's less efficient because a new object has to be created each time.
In other word, while you don't have to create an instance of the cfc first, it's often a good idea to do so.
I hope you have read OOPs and its practices. CFC is your 'blueprint' (say a car design) and object is your own data model (say a car of blue color (method to set color), with nitrogen filled tires (method to set pressure in tires) and runs on LPG (method for fuel type)). CF allow you interact directly with CFC (CFINVOKE) and you do not have to create an instance each time but it just only make sense that you would not want to go to workshop/design lab each time you want to change a configuration for your car.