I need to serialize every control of a .NET managed process (whether it's main form, child controls like buttons, calender, text boxes, list boxes, combos, etc). Every simple class object is easily serializable/deserializable using binary formaters, but these controls (which I got their info through reflection(Type.GetFields(), Type.GetProperties())) are not serializing through this serialization method which I described.
It throws a "type System.Forms.Form.WinForm to be serialize" exception. After serialization I need to pass the control's information as it is, to another remote process.
How can I get the .NET control's information to be serializable? (Json?)
I believe http://www.codeguru.com/forum/archive/index.php/t-421612.html has an answer for you
Related
To keep this simple:
I have classes defined in typescript which have methods and properties (with lots of getter/setter logic). I then retrieve json data matching such classes. I need to be able to project these json objects into my "smart" classes. I know about class transformer but I wonder if this is really go-to approach to do this kind of stuff. Furthermore, I'm planning on using ngrx, so this whole class-transformation just looks wrong (server to json, json to state, state to class? and viceversa? I just dont see a clear pattern.
Any clarity is appreciated. Thanks!
I'm doing almost exactly what you describe in a fairly large app.
I'm using class-transformer to transform the JSON from http calls to instances of the appropriate objects, and then using the resulting objects as state in a store (except that I'm using Redux instead of ngrx).
I find that it works very well.
I'm not sure exactly what you mean by "server to json, json to state, state to class? and viceversa?".
For me (using your terminology), it's server to json, json to class, class to state
(but state is just a collection of objects, i.e. class instances. I.E. state is objects).
If I need to send state back to the server, then yes, I typically pull the appropriate objects from the store, serialize them to JSON, and send them to the server. But...the Angular HttpClient does the serialization for you, so you don't typically have to write that part, unless you need some custom serialization.
I'm using asp.net core to create a webapi project.
When an object is posted to my action, I want to use the custom converter to analyse the json object first.And then create an instance of a child class. For the child, instance I only want to customize one property. So the question is, what if the object gets complex, I want to use the standard conversion to get the whole object, and manually manipulate one property.
What is the best way of achieving this?
You can add only the properties you want to your View Model, the rest will be ignored.
I eventually figured this out. For all the properties that you don't want to use custom deserialization you can still use the string.ToObject() to convert. Just do not use ToObject method on the very class that you originally created the converter for. It will craete an infinite loop trying to convert itself again and again.
In my current project we have the following list of requirements for one of our microservices:
Any object can have the list of custom validation rules and validation rules might differ between objects (it might even be that an object has the list of unique validation rules)
Those custom validation rules can be configured from UI by a user who created initial version of the object
Any time when some specific events happen with the object(changes and updates) we need to verify that the changes do not violate any of custom validation rules entered in the beginning
We also should have the list of standard checks like date range checks, number of some items and elements in the object
For now we are going to save all of this validation rules for objects either
as JSON object that will contain details of validation rule
or serialized Groovy object (*Validator)
In both cases it will be saved in database and used any time when it is needed to go throught the list of validations rules and execute them.
I am not sure that both options are the best and may be there are some other ways to implement it. Do you know any patterns or approaches that can help to implement custom validation rules?
Thank you
For custom validations implementation I'd suggest you to use Java validation API. It's very powerful in conjunction with validation groups.
You initialize beans / appropriate POJO instances based on the user inputs, and then simply call validations like: Validator.validate(T object, java.lang.Class... groups)
I want to set an arbitrary attribute for rendering to JSON.
I had followed the answer in this question: how to append data to json in ruby/rails? to do
model = Model.find
model[:extra_info] = "More detail."
model.to_json
It works perfectly, but in my tests I'm getting a deprecation warning that setting arbitrary attributes is no longer supported, use attr_writer.
I tried using
model.write_attribute(:extra_info, "More detail.")
which works in unit testing, but on the server, raises an exception:
private method `write_attribute' called for Model
What's the non-deprecated clean way to do this.
I'm aware I could set it in the JSON call with methods as in Add virtual attribute to json output, but in this case the variable to be added is not part of the models concern, so it doesn't have access to the data needed to construct the extra attribute, and it would be nasty and messy to do so.
So what's the correct way for the controller to get this data pushed into the model so the JSON renders properly?
In Model model, put
attr_accessor :extra_info
Then in controller
model.extra_info = "more detail"
Nick's answer above is the best in terms of creating well structured, well documented code.
Update
*It seems I was wrong on the below*
The code below still creates deprecation warnings on my development server.
In this particular case, I don't want to clutter the model up with extra accessors for very specific once off cases, so I'm using
s.send(:write_attribute, :extra_info, "more detail")
Inside a helper, inside the controller.
I have a custom container (C#) for the Flash ActiveX control and am passing data back and forth. Previously I would use ExternalInterface.call and pass an Array as a parameter. I would prefer to use the Vector class now that it is available, but it appears that when I do that the call is never made.
It is however made if it is embedded in IE. It appears that when in IE, Flash will send out JavaScript to execute rather than serializing to XML. My guess is that the Vector XML serialization isn't baked in, so Flash just ignores the call.
Anyone have any ideas? Other than just going back to using Array, I've already done that for now.
The docs note that:
Other built-in or custom classes -
ActionScript encodes other objects as
null or as an empty object. In either
case any property values are lost.
It's not completely clear what that means, since custom classes are also Objects - I guess only vanilla objects count? But at any rate it looks like Vector falls into that "other built-in classes" category, so you'll need to either use Array, or re-cast to Array before you pass the data.
you can use arrays with [ArrayElementType("type")] instead. or write a serialization function for Vector