c# - How store a JSON array with ServiceStack? -
i know how stored simple json message in table, how can store data in same table if generate json array? ex:
[{"id":0,"data1":123,"data2":"string1","timestamp":"/date(-62135596800000-0000)/"},{"id":0,"data1":456,"data2":"string2","timestamp":"/date(-62135596800000-0000)/"}]
i tried it doesn't work doesn't compile:
public class table1 { public list<table1> table1list{ get; set; } [autoincrement] public int id { get; set; } public int data1{ get; set; } public string data2{ get; set; } public datetime timestamp { get; set; } } // add table1 via post [route("/table1add", verbs = "post")] public class table1 add { public int id { get; set; } public int data1{ get; set; } public string data2{ get; set; } public datetime timestamp { get; set; } } // add multi table1 via post [route("/table1addmulti", verbs = "post")] public class table1 addmulti { public list<table1 > table1list { get; set; } } // store json array public object post(table1multi request) { var data =new table1() { table1list = request.table1list }; crudfunctions crud = new crudfunctions(db); return crud.adddata(data); }
i use function insert store data (crud.adddata()). function specify possible store several rows in same time.
the idea avoid send multiple message. in advance
you quite close, except shouldn't adding list<table1>
table1
. instead create table1
below, , make simple dto such table1multiple
handle multiple values.
[route("/table1","post")] public class table1 : ireturn<int> { [autoincrement] public int id { get; set; } public int data1 { get; set; } public string data2 { get; set; } public datetime timestamp { get; set; } } [route("/table1/multi","post")] public class table1multiple : ireturn<list<int>> { public list<table1> values { get; set; } }
in service should reuse code used store single record, , iterate list of table1
values
.
class testservice : service { // stores single table1 record public int post(table1 request) { // add method store single record var crud = new crudfunctions(db); return crud.adddata(request); // return id of created record } // stores multiple table1 records public list<int> post(table1multiple request) { // return list of record ids var result = new list<int>(); // process each request, through single record code foreach(var value in request.values) result.add(post(value)); return result; } }
addressing non-populated dto
i don't in "request" in debug mode.
you have 2 choices. can change request use property values
{values:[{"id":0,"data1":123,"data2":"string1","timestamp":"/date(-62135596800000-0000)/"}, {"id":0,"data1":456,"data2":"string2","timestamp":"/date(-62135596800000-0000)/"}]}
or can change request dto extend list<table1>
instead:
// [route("/table1/multi","post")] public class table1multiple : list<table1>, ireturn<list<int>> { }
then modify service reads var value in request
instead of var value in request.values
class testservice : service { // stores single table1 record public int post(table1 request) { // add method store single record var crud = new crudfunctions(db); return crud.adddata(request); // return id of created record } // stores multiple table1 records public list<int> post(table1multiple request) { // return list of record ids var result = new list<int>(); // process each request, through single record code foreach(var value in request) // collection changed here result.add(post(value)); return result; } }
Comments
Post a Comment