Kohana ORM serialize columns, JSON and stdClass - json

I'm wondering how people are dealing with the stdClass that results from json_decode run on the columns in _serialize_columns in Kohana's ORM.
The problem I'm having is that I set the column as an array, which is serialized and is all good. Later, when I retrieve it, it comes back as a stdClass. This is all good for most things, but if I attempt use something like Arr::path() on that value, it of course can't deal with (a problem with array_shift I think).
One option I tried was casting the resulting value to an array, but this only works for the first level of the array. So, I then looped through the array casting everything to an array. This worked, but seems like a considerable hack.
What is everyone else doing to deal with this?

As a recommendation from #zeelot, I filed a ticket in Kohana's ticketing system: http://dev.kohanaframework.org/issues/4188
I also overrode the Kohana_ORM method _unserialize_value() as follows:
protected function _unserialize_value($value) {
return json_decode($value, TRUE);
}
Related discussion here: http://forum.kohanaframework.org/discussion/9465/orm-serialize-columns-json-and-stdclass

Related

SwiftyJSON array elements?

I have tried finding how to work with SwiftyJSON arrays but nothing seems to work.
My function before using swift was:
json.brands.count
json.brands[0].name
json.brands[0].subname
json.brands[0].value
json.brands[1].name
json.brands[1].subname
json.brands[1].value
//Etc
Now I’m using:
json[“brands”][“name”].exists()
json[“brands”][“name”].string
But struggling to insert [0] [1] etc to make it work.
*I’m aware not to use 3rd party content if possible but my JSON api won’t work without a 3rd party. As there is 10 user inputs from a database which has thousands of dynamic entries to the JSON, so I can’t code them all, then there are objects which might be present and if they are present they might contain a value.
I tried the traditional swift json decoder approach but to catch all the errors with a dynamic nested JSON was giving me a headache.*
You can try this :
json[“brands”][0][“name”].exists()
json[“brands”][0][“name”].string
json[“brands”][1][“name”].exists()
json[“brands”][1][“name”].string
Hope it helps...

Deserialize an anonymous JSON array?

I got an anonymous array which I want to deserialize, here the example of the first array object
[
{ "time":"08:55:54",
"date":"2016-05-27",
"timestamp":1464332154807,
"level":3,
"message":"registerResourcePath ('', '/sap/bc/ui5_ui5/ui2/ushell/resources/')",
"details":"","component":"sap.ui.ModuleSystem"},
{"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":"URL prefixes set to:","details":"","component":"sap.ui.ModuleSystem"},
{"time":"08:55:54","date":"2016-05-27","timestamp":1464332154808,"level":3,"message":" (default) : /sap/bc/ui5_ui5/ui2/ushell/resources/","details":"","component":"sap.ui.ModuleSystem"}
]
I tried deserializing using CL_TREX_JSON_SERIALIZER, but it is corrupt and does not work with my JSON, here is why
Then I tried /UI2/CL_JSON, but it needs a "structure" that perfectly fits the object given by the JSON Object. "Structure" means in my case an internal table of objects with the attributes time, date, timestamp, level, messageanddetails. And there was the problem: it does not properly handle references and uses class description to describe the field assigned to the field-symbol. Since I can not have a list of objects but only a list of references to objects that solution also doesn't works.
As a third attempt I tried with the CALL TRANSFORMATION as described by Horst Keller, but with this method I was not able to read in an anonymous array, and here is why
My major points:
I do not want to change the JSON, since that is what I get from sap.ui.log
I prefere to use built-in functionality and not a thirdparty framework
Your problem comes out not from the anonymity of array, but from the awkwardness of SAP JSON (De)serializer, which doesn't respect double quotes, which enclose JSON attributes. The issue is thoroughly described in this answer.
If you don't want to change your JSON on-the-fly, the only way you have is to change CL_TREX_JSON_DESERIALIZER class like this.
/UI5/CL_JSON_PARSER parses JSONs with unknown format.
Note that it's got "for internal use" written on it so many times that you probably should take it seriously and clone its code to fixate it.

Spray JSON - deserialize field that may have different values

I am facing a problem of parsing a JSON with a field that may be Array or single object. In case hotel has multiple rooms, typical JsArray is returned. However, if it has only one type of room, it returns only one single object.
I am trying to parse this situation using case classes, especially List[Room] for this situation. This however fails in case only one single object is returned. Is there a way how to overcome this with case classes? If not, what is the solution to this problem?
Thank you very much.
You can use Either[A,B], Either can contain either instance of A or instance of B
case class Hotel(data: Either[Room, List[Room]])

Removing an entry from a GWT JSONObject

Let's say I have a JSONObject in GWT that looks like this: {"name1":value1, "name2":value2}. Is there a way to remove the "name2":value2 key/value pair and change this object to {"name1":value1}? I have not found any methods that help with this approach in the GWT Javadoc.
I know there are workarounds to this, of course. Since my JSONObject is small, I am currently making a new one and putting in it all the key/value pairs other than the one I want to remove. But this won't work when I plan to pass in the JSONObject to a child function; since only the JSONObject's reference is passed in Java, I need a mutator function to actively change what the method parameter's JSONObject points to. In the worse case, I could convert the JSONObject to a String and regexp out what I don't want. But this seems prone to error and ugly. Any suggestions?
Actually, put()ing a null (as opposed to a JSONNull) value will delete the value for the given key.

Casting objects created in LINQ to SQL to a single master object

I have an interesting problem to solve that would be helped by successfully casting objects created by LINQ to SQL into a single master object that I could pass around. Here is the scenario at a high level.
I have a number of stored procedures that fetch data and then all return the exact same columns. The params into the procs and the logic vary greatly, so a single proc will not work. Then Linq creates a strongly typed object which is used throughout my application as parameter and return values.
I am using these strongly typed objects as noted above as parameters and return values in a series of filters used to analyze stocks. My client would like to change the order the order of the filters. The issue is that each succeeding filter will only work on what passed the last filter.
Currently I am hard coding my parameters, and if I could create a master object that I could cast any of these Linq objects to, I could then always pass and return the master object.
I have read the materials available on the internet about casting between different types such as static to anonymous types or a list of integers and an array list containing objects representing integers, but I need to actually cast one object into another.
What general direction would I take to solve this problem of converting strongly typed objects generated by linq that are exactly the same into a single master object?
Thank you for any of your thoughts.
If all your linq objects have the same fields, you could have them implement an interface defined with those common fields. Then the calls to your filter methods can depend on an interface rather than a specific implementation. In other words, the parameters in the filter methods will be of the interface type rather than a linq class type.
e.g.: Where ICommonFields is an interface you define with all the common fields in each l2s class -
public class Filterer
{
public ICommonFields filterStuff(ICommonFields x)
{
//do stuff
}
}
or -
public class Filterer
{
public T filterStuff<T>(T x)
where T: class, ICommonFields, new()
{
//do stuff
}
}
I'd prefer the generic version, as T becomes the actual type rather than a reference through an interface - linq-to-sql has issues when using a type through an interface with query expressions.
Edit: sorry, it was late when i first wrote this response (likely excuse! :). Fixed my obvious mistake with the example code :)
Although there might be a way to do this with casting, I'm going to offer you a quick and dirty solution - and I'm assuming that your resultant objects are collection-based:
Given that all of your child objects
all share the same columns, go ahead
and pick one of them to act as your
master object - then simply iterate
through the rows of your other LINQ
objects and add them to the collection
of your master object. If your
resultant object is a strongly typed
data table, then all you'd do is Add
to the .Rows collection.
Additionally, you might be able to just add the elements retrieved some subsequent LINQ queries directly to your master object depending upon how you write your SELECT causes in LINQ.