In a slim project I use the MySqliDb wrapper class, which provides an objectBuilder function to retrieve the data as a StdClass object instead of an associative array.
Is there any way to retrieve data as a custom class instance similarly to what mysqli fetch_object("MyCustomClass") does?
Thanks
Related
I saw a tutorial where an extra class is created just to be able to serialize a list of objects, instead of a single object:
I'm using json_serializable to generate some serialization code for my class Preference, but now I want to save a list of preferences using shared_preferences and I get an error obviously.
var sSavedPrefs = json.encode(PreferenceRepo.getSavedPrefs());
prefs.setString(saved_prefs_key, sSavedPrefs );
I used
#JsonSerializable()
class Preference{...}
to make it serializable, but I don't want to create an extra class like
#JsonSerializable()
class Preferences{...}
just to make it work - is there a better way?
I found a way:
Using the setStringList method I could create a List where I added each serialized object one by one without needing an extra list class. I also noticed that json.encode might not have been the right method to use, I saw jsonEncode in another tutorial and used it instead:
List<String> savedPrefsJson = [];
for (Preference savedPref in PreferenceRepo.savedPrefs) {
String savedPrefJson = jsonEncode(savedPref);
savedPrefsJson.add(savedPrefJson);
}
prefs.setStringList(saved_prefs_key, savedPrefsJson);
Is there an elegant way to convert an object that contains prototypes to an object with properties only? (all prototype methods would be omitted)
In one line:
_.omit(myObj, _.isFunction);
Also with JSON:
JSON.parse(JSON.stringify(myObj));
https://jsfiddle.net/yh2utkdw/1/
I'm using Protostuff in an attempt to serialize/deserialize objects of several different types for which no protobuf sources are available (it's a server-server RPC scenario). Serialization goes OK because I know the type of the object to serialize and can create the schema:
Schema schema = RuntimeSchema.getSchema(object.getClass());
Now, I use ProtobufIOUtil.toByteArray and get a byte array which I then pass to a remote server. However, I can't seem to deserialize this byte array in the remote server because I have no way to create a schema for an object of "unknown" type. Is there any way I can get past this and use Protostuff in the same way I would use Java's native serialization?
There are few solutions with common idea - serialize name of the class together with the data.
First one requires protostuff-runtime. You should create wrapper class with one field of type Object:
public class Wrapper {
public Object data;
}
Then you put your object to data field and serialize wrapper, protostuff-runtime will append class name to serialized form automatically, and later use it for deserialization.
If you want more control, then you can do similar thing without protistuff-runtime.
First, you need a wrapper class:
public class Wrapper {
public String clazz;
public byte[] data;
}
Then you should serialize your data to byte array, store it to wrapper, and then serialize wrapper instance.
On remote side, you deserialize Wrapper first, then get clazz field - it is the class you should use to deserialize data.
I think what I'm looking for is something very simple, yet I am unable to find any examples.
I'd like to use Ninject to create an object by having Ninject call a factory method with a parameter specified and not injected during the actual request to instantiate the object:
Request for an object here:
StandardKernel.Get<ISomeInteface>(new Ninject.Parameters.Parameter("dataContext", dataContext, true));
And I'd like to map the ISomeInterface to a method that is expecting a value to be passed to it at runtime.
Mapping an interface here:
Kernel.Bind<ISomeInterface>().ToMethod(SomeObject.Create(--> `what do I put here?`));
Is this possible? If so, how do I properly map my interface?
Thanks!
ToMethod(ctx =>
SomeObject.Create(
(IDataContext)ctx.Parameters.Single(p =>p.Name == "dataContext")
.GetValue(ctx, null))
But you should rethink your design to avoid calling Get from anywhere else than your composite root.
I've written a implementation of Converter that uses Jackson JSON to serialize a list of deeply structured objects to JSON, but it's not being called. Am I doing this correctly?
#Converter(name="arrayList", converterClass=ArrayListJsonSerializedObjectConverter.class)
private List<DeeplyStructuredObject> deeplyStructuredObjects= new ArrayList<DeeplyStructuredObject>();
Additionally, I'd like to know whether or not one can use a ReadTransfomer in addition to a
Converter on the same property, e.g.
#Converter(name="arrayList", converterClass=ArrayListJsonSerializedObjectConverter.class)
#ReadTransformer(method="someMethod")
private List<DeeplyStructuredObject> deeplyStructuredObjects= new ArrayList<DeeplyStructuredObject>();