【web Api性能提升技巧】(2)从DataReader手工创建Json字符串
这个思路是从 一篇文章,关于《提升web api的性能》上看到的。自己实践了一番,写下步骤。
传统的DataReader是遵循这样的一个步骤:
While(reader.Read())
{
//创建对象,赋值,添加到集合
}
//返回Json.序列化(集合)
现在我们采用的是手工拼接Json字符串:通过解析DataReader的数据格式、内容,采用StringBuilder.Append这种方式进行手工拼接。
避免了每次初始化对象、序列化集合所带来的内存、时间上的消耗。在查询数据量很大集合时,很有帮助。
代码很简单,就不多做解释了
using System;
using System.Data.SqlClient;
using System.Text; namespace DataProvider.Common
{
public class Tools
{
/// <summary>
/// 将Reader集合转换成字符串
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static string ConvertReadersToJson(SqlDataReader reader)
{
var jsonStr = new StringBuilder();
jsonStr.Append("["); while (reader.Read())
{
ConvertSingleReaderToJson(reader, jsonStr);
jsonStr.Append(",");
} if (jsonStr.Length > )
{
jsonStr.Length = jsonStr.Length - ;
jsonStr.Append("]");
return jsonStr.ToString();
} return string.Empty;
} /// <summary>
/// 将一个Reader转换成Json字符串,reader必须可读.read
/// </summary>
/// <param name="reader"></param>
/// <param name="jsonStr"></param>
public static void ConvertSingleReaderToJson(SqlDataReader reader, StringBuilder jsonStr)
{
jsonStr.Append("{"); for (var i = ; i < reader.FieldCount; i++)
{
//值类型数据不需要引号,否则需要
var fieldType = reader.GetFieldType(i);
object fileValue = reader[i]; if (fieldType == typeof(DateTime))
{
DateTime dt = DateTime.MinValue; if (DateTime.TryParse(fileValue.ToString(), out dt))
{
fileValue = ConvertDateTimeToJson(dt);
}
}
//日期作为特殊情况已经处理过了
var comma = (fieldType == typeof(string) || fieldType == typeof(Guid)) ? "\"" : "";
jsonStr.AppendFormat("\"{0}\":{2}{1}{2},", reader.GetName(i), fileValue, comma);
}
//去掉多余的逗号
jsonStr.Length = jsonStr.Length - ;
jsonStr.Append("}");
} /// <summary>
/// 将.net 日期格式转换成Json日期格式
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static string ConvertDateTimeToJson(DateTime dt)
{
DateTime d1 = new DateTime(, , );
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return string.Format("\"/Date({0})/\"", ts.TotalMilliseconds.ToString("#"));
}
}
}
PS:另外。请教下各位朋友,如何去除Web Api默认托管的Json.net序列化框架,改用自己手工写Json呢?
也就是说,Controller传入、传出的对象是String类型,我自己来负责解析?
【web Api性能提升技巧】(2)从DataReader手工创建Json字符串的更多相关文章
- 8 种提升 ASP.NET Web API 性能的方法
ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能. 在本文中,我将介绍8项提高 ASP.NET Web ...
- 六种简单方法提升ASP.NET Web API性能
ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能. 在本文中,我将介绍8项提高 ASP.NET Web ...
- 8种提升ASP.NET Web API性能的方法
英文原文:8 ways to improve ASP.NET Web API performance ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没 ...
- [转载]8 种提升 ASP.NET Web API 性能的方法
http://www.oschina.net/translate/8-ways-improve-asp-net-web-api-performance 英文原文:8 ways to improve A ...
- 8 种提升 ASP.NET Web API 性能的方法 (转)
出处:http://www.oschina.net/translate/8-ways-improve-asp-net-web-api-performance ASP.NET Web API 是非常棒的 ...
- 8 种提升ASP.NET Web API性能的方法
ASP.NET Web API 是非常棒的技术.编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能. 在本文中,我将介绍8项提高 ASP.NET Web ...
- Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案
Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案 1. 业务场景 android+webview h5 css背景图性能提升1 2. ...
- 使用Jil序列化JSON提升Asp.net web api 性能
JSON序列化无疑是Asp.net web api 里面性能提升最重要的一环. 在Asp.net web api 里面我们可以插入自定义的MediaTypeFormatter(媒体格式化器), 说白了 ...
- Web 应用性能提升 10 倍的 10 个建议
转载自http://blog.jobbole.com/94962/ 提升 Web 应用的性能变得越来越重要.线上经济活动的份额持续增长,当前发达世界中 5 % 的经济发生在互联网上(查看下面资源的统计 ...
随机推荐
- 【RF库测试】对出错的处理
1.出错后继续执行:Run Keyword And Continue On Failure 2.获取关键字执行结果后继续执行:Run Keyword And Ignore Error 有时候,我们需要 ...
- c++ rand()
一.C++中不能使用random()函数 random函数不是ANSI C标准,不能在gcc,vc等编译器下编译通过.但在C语言中int random(num)可以这样使用,它返回的是0至num-1的 ...
- zoj3686(线段树的区间更新)
对线段树的区间更新有了初步的了解... A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a ...
- [Android] 开源框架 xUtils HttpUtils 代理设置 (Temporary Redirect错误)
今天简单学习了一下xUtils的使用 https://github.com/wyouflf/xUtils 其中用到HttpUtils模块时,发现总是出现Temporary Redirect 错误. 查 ...
- c#基础 第八讲
static void Main(string[] args) { while (true)//一直循环 { Random r = new Random();//创建随机函数r int[] caipi ...
- 160420、zTree获取所有选中节点数据
<!DOCTYPE html><HTML><HEAD> <TITLE> ZTREE DEMO - Standard Data </TITLE> ...
- SpringMVC的解释与搭建Maven私有代理服务器
SpringMVC静态资源处理 通常会配置SpringMVC拦截所有请求 即将DisptcherServlet的url-pattern设置为 / 此时会导致SpringMVC同时拦截.css .j ...
- Foj1683矩阵快速幂水题
Foj 1683 纪念SlingShot 题目链接:http://acm.fzu.edu.cn/problem.php?pid=1683 题目:已知 F(n)=3 * F(n-1)+2 * F(n-2 ...
- Convolution Matrix
w褶积矩阵.二值化旧图经核矩阵得到新图. https://docs.gimp.org/en/plug-in-convmatrix.html 8.2. Convolution Matrix 8.2.1. ...
- 购物车删除商品,总价变化 innerHTML = ''并没有删除节点,内容仍存在
w元素的上的下. function deleteLi(tmpId) { //document.getElementById(tmpId).innerHTML = ''; var wdel = docu ...