我想要一种尽可能自动地将对象序列化和反序列化为JSON的方法.
序列化:
对我来说,理想的方法是,如果我在实例中调用JSONSerialize(),它将返回一个带有JSON对象的字符串,该对象具有该对象的所有公共属性"name_of_property": "value".对于那些作为原语的值,它很简单,对于它应该尝试在每个JSONSerialize()或ToString()上调用的对象来递归序列化所有公共属性.对于集合,它也应该正常运行(只需矢量/数组就可以了).
反序列化:只需创建给定对象的实例(让我们说一只狗)并调用JSONDeserialize(json_string),这应该填充所有公共属性,在属性不是基元或需要的集合的情况下创建所需的对象.
一个例子应该像这样运行:
Dog *d1 = new Dog();
d1->name = "myDog";
string serialized = d1->JSONSerialize();
Dog *d2 = new Dog();
d2->JSONDeserialize(serialized);
std::cout << d2->name; // This will print "myDog"
Run Code Online (Sandbox Code Playgroud)
或者像那样:
Dog *d1 = new Dog();
d1->name = "myDog";
string serialized = JSONSerializer.Serialize(d1);
Dog *d2 = JSONSerializer.Deserialize(serialized, Dog);
std::cout << d2->name; // This will print "myDog"
Run Code Online (Sandbox Code Playgroud)
我该如何轻松地将其拉下来?
我设计了一个控制器ErrorController,使用类似Forbiddenor 的方法调用NotFound,因此我可以将以下几行添加到 Web.config 中:
<customErrors mode="On" defaultRedirect="~/Error/Unknown" />
<error statusCode="404" redirect="~/Error/NotFound" />
<error statusCode="403" redirect="~/Error/Forbidden" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
所以现在我希望能够做这样的事情:
public ActionResult Edit(int idObject)
{
if( user.OnwsObject(idObject) )
{
// lets edit
}
else
{
// ** SEND AN ERROR 403 ***
// And let ASP.NET MVC with IIS manage that error to send
// the requester to the Web.config defined error page.
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我尝试过类似的事情:(A)throw new HttpException(403, "Error description");:导致导致系统崩溃的未处理异常,(B)return HttpStatusResultCode(403, "Error description") …
我创建了一个类 User.它有一些属性,如:名称,ID,密码等.
我已经设置了一个构造函数:
function User(name1, email, password) {
this.name = name1;
this.id = __GUID();
this.type = __TYPE;
this.addNewEmail(email);
this.password = (new Password).createFromRawPassword(password).crypt;
}
Run Code Online (Sandbox Code Playgroud)
一些原型功能如:
User.prototype.save = function(cb, callback) {
return cb.bucket.upsert(this.id, this, function(err, res) {
if (callback == null) {
return callback(err, res);
}
});
};
User.prototype.addNewEmail = function(email) {
this.emails = this.emails || [];
return this.emails.push(email);
};
Run Code Online (Sandbox Code Playgroud)
这对我来说非常好用,让我将我的对象存储为序列化的JSON(在带有NodeJS的couchbase数据库中),而不在对象中包含函数.
当我从DB获取对象时出现问题.它带有所有属性,如存储,但我需要添加功能.我试图使用extend我发现的mixins的一些函数,但是他们将函数添加为ownProperties,因此,下次我将更新的记录保存到DB时,我得到了保存的函数.
如何将接收的对象再次转换为User类型的对象?要使类的必需方法也出现在实例上.
我正在创建一个应用程序,我想在按热键时向用户显示"弹出"或"通知".
我希望它显示为浮动消息,如更改音量或亮度时显示的消息,或者像Quicksilver App输入窗口一样.
我希望它有阿尔法,淡入,在那里停留5-8秒,并淡出.
而且......问题是我不知道如何开始这个.我试过了:
NSRect frame = NSMakeRect(0, 0, 600, 65);
_myWindow = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[_myWindow setBackgroundColor:[NSColor blueColor]];
[_myWindow center];
[_myWindow makeKeyAndOrderFront:NSApp];
Run Code Online (Sandbox Code Playgroud)
但是,当然这不是我想要做的.
欢迎任何帮助;-)
我知道有很多类似的问题,但我没有得到任何我想要的答案.我希望通过执行类似的操作将ActionResult转换为String:
public ActionResult ProductDetail(int id)
{
// ... lots of operations that include usage of ViewBag and a Model ...
return View(product);
}
Run Code Online (Sandbox Code Playgroud)
要向客户端显示产品页面,我希望能够通过JSON发送它,例如:
public JsonResult ProductDetailJSON(int id)
{
// ... lots of operations that include usage of ViewBag and a Model ...
return Json(new {
html = this.ProductDetail(id).ToString(),
name = ""
// ... more properties
}, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
因此,我将调用 Controller Action并获取其响应(a ActionResult,实际上是a ViewResult)并将其转换为a String,但是使用控制器方法的所有业务代码(包括,特别是ViewBag用法)创建它.
编辑澄清为什么我想这样做
我需要能够在浏览器中显示产品,因此我创建了一个名为的Controller Action ProductDetail,并且工作得很好.
在此之后需要一个API,为给定的产品提供一个带有一些元数据(如销售单位,名称等)和View的JSON对象.一种选择是发送一个链接到视图,这就是全部...但视图必须作为客户端要求内联.
为了避免代码重复并保持代码清洁,我想调用该方法ProductDetail,并捕获HTML …
我想知道是否有办法通过我的应用程序的单次往返在 redis 中执行此操作:
对于给定的键K,其可能的值V是范围内的任何整数[A, B]。基本上,它有一个上下边界。
当发出INCRBYorDECRBY命令时(例如INCRBY key 10),只有当结果值没有超出范围时才会执行。
我需要这个操作是原子的,我想知道是否有办法避免为此编写 Lua 脚本。
谢谢你。
我正在创建一个通过HTTPS进行通信的Web服务.
安全性对我来说非常重要,因为我无法接受修改后的数据,因此我正在考虑添加消息diggest(签名)以检查完整性......但它是否需要通过HTTPS进行?