Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an object to a JSON string with thrift json serialization

I'm new to the thrift. I need to convert my data object to a JSON string with Thrift JSON serialization.

I tried in this way.

TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
String json = serializer.toString(object_name);

In here is an error, that object_name should be in TBase. How can I resolve this ?

like image 537
Shashika Avatar asked Feb 06 '14 08:02

Shashika


People also ask

Can JSON object convert to string?

A JSONObject has several methods such as get , opt and toString . If you have a JSONObject , you can easily convert it into a String using toString method. If you absolutely need JACKSON, you can try: JSONObject object = ...; ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.

How do I convert a JSON to a string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Is converting to JSON serialization?

Serialization is the process of converting . NET objects such as strings into a JSON format and deserialization is the process of converting JSON data into .

What is the difference between JSON and serialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.


2 Answers

In here is an error, that object_name should be in TBase.

Next time, please post the exact error message (use copy+paste), this makes it easier for all of us.

How can I resolve this?

Whatever you want to serialize with Thrift, must be an descendant of Thrift's TBase class. You achieve this by writing some Thrift IDL and save it as a file (e.g. MyDataStructs.thrift):

struct Employee {
    1: string name
    2: string surname
    3: i32 age
}

Next, you pass that file to the Thrift compiler and tell him to generate some C# code from it:

thrift  -gen csharp  MyDataStructs.thrift

This gives you a class derived from TBase:

public partial class Employee : TBase
{
  private string _name;
  private string _surname;
  private int _age;

  // properties
  public string Name {... }
  public string Surname  { ... }
  public int Age  { ... }

  // some details omitted

  public void Read (TProtocol iprot)
  {
    // generated code for Read() method
  }

  public void Write(TProtocol oprot) {
    // generated code for Write() method
  }

  public override string ToString() {
    // generated code for ToString() method
  }

}

This is what Thrift expects.

like image 81
JensG Avatar answered Oct 05 '22 07:10

JensG


If below is what your are doing then it should work. Check if you are doing this. Employee is a demo call here, you have to use your actual class.

Employee object_name= new Employee();
object_name.setAge(27);
object_name.setName("Test");

TSerializer serializer = new TSerializer(new TSimpleJSONProtocol.Factory());
String json = serializer.toString(object_name);
like image 39
A Paul Avatar answered Oct 05 '22 06:10

A Paul