Convert an object to a JSON string using siftift json serialization

I'm new to frugality. I need to convert my data object to a series JSON stringwith Thrift JSON.

I tried this way.

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

Here is the error that object_nameshould be in TBase. How can i solve this?

+3
source share
2 answers

Here is the error that object_name must be in TBase.

Next time, please write the exact error message (use copy + paste), it will simplify for all of us.

How can i solve this?

, Thrift, Thrift TBase. , Thrift IDL (, MyDataStructs.thrift):

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

Thrift #:

thrift  -gen csharp  MyDataStructs.thrift

, 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
  }

}

, .

+4

, . , . - , .

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);
+1

All Articles