If all of the objects you would be returning are derived from a common base class, you could use a std::map<std::string,BaseClass *>. The comparisons ultimately in there somewhere, but it keeps things better organized.
Videos
If all of the objects you would be returning are derived from a common base class, you could use a std::map<std::string,BaseClass *>. The comparisons ultimately in there somewhere, but it keeps things better organized.
No, you can't do it with standard C++. C++ has no notion of reflection. Sorry :)
.c_str() returns a const char*. If you need a mutable version, you will need to produce a copy yourself.
vector<char> toVector( const std::string& s ) {
vector<char> v(s.size()+1);
std::memcpy( &v.front(), s.c_str(), s.size() + 1 );
return v;
}
vector<char> v = toVector("apple");
// what you were looking for (mutable)
char* c = v.data();
.c_str() works for immutable. The vector will manage the memory for you.
You can use the Newtonsoft.Json library as follows:
using Newtonsoft.Json;
...
var result = JsonConvert.DeserializeObject<T>(json);
Where T is your object type that matches your JSON string.
It looks like you're trying to deserialize to a raw object. You could create a Class that represents the object that you're converting to. This would be most useful in cases where you're dealing with larger objects or JSON Strings.
For instance:
class Test {
String test;
String getTest() { return test; }
void setTest(String test) { this.test = test; }
}
Then your deserialization code would be:
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
Test routes_list =
(Test)json_serializer.DeserializeObject("{ \"test\":\"some data\" }");
More information can be found in this tutorial: http://www.codeproject.com/Tips/79435/Deserialize-JSON-with-Csharp.aspx
The two are intended for different purposes. The ToString method of any object is supposed to return a string representation of that object. Casting is quite different, and the 'as' key word performs a conditional cast, as has been said. The 'as' key word basically says "get me a reference of this type to that object if that object is this type" while ToString says "get me a string representation of that object". The result may be the same in some cases but the two should never be considered interchangeable because, as I said, they exist for different purposes. If your intention is to cast then you should always use a cast, NOT ToString.
from http://www.codeguru.com/forum/showthread.php?t=443873
see also http://bytes.com/groups/net-c/225365-tostring-string-cast
If you know it is a String then by all means cast it to a String. Casting your object is going to be faster than calling a virtual method.
Edit: Here are the results of some benchmarking:
============ Casting vs. virtual method ============
cast 29.884 1.00
tos 33.734 1.13
I used Jon Skeet's BenchmarkHelper like this:
using System;
using BenchmarkHelper;
class Program
{
static void Main()
{
Object input = "Foo";
String output = "Foo";
var results
= TestSuite.Create("Casting vs. virtual method", input, output)
.Add(cast)
.Add(tos)
.RunTests()
.ScaleByBest(ScalingMode.VaryDuration);
results.Display(ResultColumns.NameAndDuration | ResultColumns.Score,
results.FindBest());
}
static String cast(Object o)
{
return (String)o;
}
static String tos(Object o)
{
return o.ToString();
}
}
So it appears that casting is in fact slightly faster than calling ToString().
I am storing a class object into a string using toString() method. Now, I want to convert the string into that class object.
Your question is ambiguous. It could mean at least two different things, one of which is ... well ... a serious misconception on your part.
If you did this:
SomeClass object = ...
String s = object.toString();
then the answer is that there is no simple way to turn s back into an instance of SomeClass. You couldn't do it even if the toString() method gave you one of those funky "SomeClass@xxxxxxxx" strings. (That string does not encode the state of the object, or even a reference to the object. The xxxxxxxx part is the object's identity hashcode. It is not unique, and cannot be magically turned back into a reference to the object.)
The only way you could turn the output of toString back into an object would be to:
- code the
SomeClass.toString()method so that included all relevant state for the object in the String it produced, and - code a constructor or factory method that explicitly parsed a String in the format produced by the
toString()method.
This is probably a bad approach. Certainly, it is a lot of work to do this for non-trivial classes.
If you did something like this:
SomeClass object = ...
Class c = object.getClass();
String cn = c.toString();
then you could get the same Class object back (i.e. the one that is in c) as follows:
Class c2 = Class.forName(cn);
This gives you the Class but there is no magic way to reconstruct the original instance using it. (Obviously, the name of the class does not contain the state of the object.)
If you are looking for a way to serialize / deserialize an arbitrary object without going to the effort of coding the unparse / parse methods yourself, then you shouldn't be using toString() method at all. Here are some alternatives that you can use:
- The Java Object Serialization APIs as described in the links in @Nishant's answer.
- JSON serialization as described in @fatnjazzy's answer.
- An XML serialization library like XStream.
- An ORM mapping.
Each of these approaches has advantages and disadvantages ... which I won't go into here.
Much easier way of doing it: you will need com.google.gson.Gson for converting the object to json string for streaming
to convert object to json string for streaming use below code
Gson gson = new Gson();
String jsonString = gson.toJson(MyObject);
To convert back the json string to object use below code:
Gson gson = new Gson();
MyObject = gson.fromJson(decodedString , MyObjectClass.class);
Much easier way to convert object for streaming and read on the other side. Hope this helps. - Vishesh
Method below expects values to be supplied as object[], however, i have my values in string[]. The following works with hard-coded values.
var ADS_PROPERTY_DELETE = 4;
var users = new object[] {
"CN=testUser1,OU=LabUsers,DC=Lab1,DC=contoso,DC=local",
"CN=testUser2,OU=LabUsers,DC=Lab1,DC=contoso,DC=local"
}
oDE.Invoke("PutEx", new object[] {ADS_PROPERTY_DELETE, "member", users});I am unable to find a way to convert my string[] to object[].
Appreciate any assistance.
I want a function that takes two strings as arguments. Once is a string value, the second is the name of the string property as a string. Example: function("hello world", "ToUpper")
Now "hello world".ToUpper() should be called.
I dont have any usage for this, I was just wondering if this is possible?