700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 基于android开发的手机安全卫士

基于android开发的手机安全卫士

时间:2023-12-14 23:10:53

相关推荐

基于android开发的手机安全卫士

随着智能手机和网络的完美结合,使得智能机的功能越来越强大,浏览网页、网络购物、视频对话都普及到各个手机终端,然而手机平台越广泛,存在的危险就越大,越来越多的安全问题出现在手机的日常运用中。当我们以为只有通过网络才会使手机中毒的时候,没想到智能手机那方便快捷的数据传送方式:WIFI、蓝牙、内存卡等也使我们的手机暴露在病毒感染的危险之中。通讯设备本身所具备的便利的支付收费、支付方式都为不法份子提供了可乘之机。

文件:/f/25127180-739631482-66240e?p=551685 (访问密码: 551685)

目前基本所有的序列化和反序列化都是基于反射,反射是运行时的一些操作,一直以来性能差而被诟病。System.Text.Json中的JsonSerializer对象中的序列化操作也是基于反射的,我们常用的方法如下:

序列化:

JsonSerializer.Serialize(student, new JsonSerializerOptions()

{

WriteIndented = true,

PropertyNameCaseInsensitive = true //不敏感大小写

});

反序列化:

JsonSerializer.Deserialize(“xxxx”);

本身微软就宣称System.Text.Json.JsonSerializer性能是强于一个Newtonsoft,所以这两年一直使用微软自带的。

当然话题扯远了,只是带大家稍微了解回顾下。

我们来看看微软官网提供的反射和源生成两种方式在Json序列化中的优劣:

1.可以看到反射的易用性和开放程度是高于源生成的。

2.性能方面则是源生成完全碾压。

源生成注意点

1.源生成有两种模式:元数据收集和序列化优化,两者的区别会在下面的实践中给出自己的理解,官网并没有得到较为明确的两种的解释,两种生成模式可以同时存在。默认同时启用。

2.源生成不能够像反射一样可以使用JsonInclude标签将包含私有访问器的公共属性包含进来,会抛NotSupportedException异常

元数据收集&序列化优化

元数据收集

可以使用源生成将元数据收集进程从运行时移到编译时。 在编译期间,系统将收集元数据并生成源代码文件。 生成的源代码文件会自动编译为应用程序的一个整型部分。 使用此方法便无需进行运行时元数据集合,这可提高序列化和反序列化的性能.

序列化优化:

这个就比较好理解一点了,无非就是对于序列化的一些设置选项和特性做出一些优化,当然目前不是所有设置和特性都支持,官网也列出了受支持的设置和特性。

设置选项:

特性:

好了说了这么多,大家对一些概念都有了基本了解,我也很讨厌这么多文字的概念往上贴,那么现在就进入实战!

实战

创建项目

一个.net6的控制台项目,可以观察到它的分析器里有一个System.Text.Json.SourceGenerator这个解析器

创建一个序列化上下文

创建SourceGenerationContext派生自JsonSerializerContext

指定要序列化或反序列化的类型

通过向上下文类应用 JsonSerializableAttribute 来指定要序列化或反序列化的类型。

不需要为类型的字段类型做特殊处理,但是如果类型包含object类型的对象,并且你知道,在运行时,它可能有 boolean 和 int 对象

则需要添加

[JsonSerializable(typeof(bool))]

[JsonSerializable(typeof(int))]

以增加对于这些类型的支持,便于源生成提前生成相关类型代码。

序列化配置

JsonSourceGenerationOptions可以添加一些序列化的配置设置。

序列化上下文最后代码:

[JsonSourceGenerationOptions(WriteIndented = true)]

[JsonSerializable(typeof(Student))]

[JsonSerializable(typeof(Teacher))]

internal partial class SourceGenerationContext : JsonSerializerContext

{

}

分析器下会出现一些自动生成的代码:

序列化/反序列化

序列化:

JsonSerializer.Serialize(student, SourceGenerationContext.Default.Student);

反序列化:

var obj = JsonSerializer.Deserialize(

jsonString, SourceGenerationContext.Default.Student);

指定源生成方式

元数据收集模式

全部类型设置元数据收集模式

[JsonSourceGenerationOptions(WriteIndented = true,GenerationMode =JsonSourceGenerationMode.Metadata)]

[JsonSerializable(typeof(Student))]

[JsonSerializable(typeof(Teacher))]

internal partial class SourceGenerationContext : JsonSerializerContext

{

}

单个类型设置元数据收集模式,只设置学生类型使用特定的元数据收集模式

[JsonSourceGenerationOptions(WriteIndented = true,GenerationMode =JsonSourceGenerationMode.Metadata)]

[JsonSerializable(typeof(Student,GenerationMode =JsonSourceGenerationMode.Metadata))]

[JsonSerializable(typeof(Teacher))]

internal partial class SourceGenerationContext : JsonSerializerContext

{

}

序列化优化模式

全部类型设置序列化优化模式

[JsonSourceGenerationOptions(WriteIndented = true,GenerationMode =JsonSourceGenerationMode.Serialization)]

[JsonSerializable(typeof(Student))]

[JsonSerializable(typeof(Teacher))]

internal partial class SourceGenerationContext : JsonSerializerContext

{

}

单个类型设置序列化优化模式,只设置学生类型使用特定的序列化优化模式

[JsonSourceGenerationOptions(WriteIndented = true)]

[JsonSerializable(typeof(Student), GenerationMode = JsonSourceGenerationMode.Serialization)]

[JsonSerializable(typeof(Teacher))]

internal partial class SourceGenerationContext : JsonSerializerContext

{

}

注意点:如果不显示设置源生成模式,那么会同时应用元数据收集和序列化优化两种方式。

效果对比

说了这么多,你凭啥说服我们使用这玩意儿??

我们试试使用JsonSerializer和源生成的方式来跑10000次序列化试试,说试就试,完整代码如下:

using ponentModel.DataAnnotations;

using System.Diagnostics;

using System.Text.Json;

using System.Text.Json.Serialization;

namespace DemoSourceGenerator

{

public class Student

{

public int Id { get; set; }

public string StuName { get; set; }

public DateTime Birthday { get; set; }

public string Address { get; set; }

}

public class Teacher{public int Id { get; set; }public string TeacherName { get; set; }public DateTime Birthday { get; set; }public string Address { get; set; }}[JsonSourceGenerationOptions(WriteIndented = true)][JsonSerializable(typeof(Student))][JsonSerializable(typeof(Teacher))]internal partial class SourceGenerationContext : JsonSerializerContext{}public class Program{public static void Main(string[] args){Student student = new Student(){Id = 1,StuName = "Bruce",Birthday = DateTime.Parse("1996-08-24"),Address = "上海市浦东新区"};var jsonOptions = new JsonSerializerOptions(){WriteIndented = true,PropertyNameCaseInsensitive = true};Stopwatch stopwatch1 = new Stopwatch();stopwatch1.Start();foreach (var index in Enumerable.Range(0, 100000)){JsonSerializer.Serialize(student, jsonOptions);}stopwatch1.Stop();Console.WriteLine($"原始的序列化时间:{stopwatch1.ElapsedMilliseconds}");Stopwatch stopwatch2 = new Stopwatch();stopwatch2.Start();foreach (var index in Enumerable.Range(0, 100000)){JsonSerializer.Serialize(student, SourceGenerationContext.Default.Student);}stopwatch2.Stop();Console.WriteLine($"源码生成器的序列化时间:{stopwatch2.ElapsedMilliseconds}");}}

}

我们直接跑这个程序看看

跑了几次下来,时间差距都在两倍左右,当然按照官方所说,内存等方面也会有大幅度优化。

应用场景

1.首先肯定是.net 6及其之后的版本,因为我们公司在升级一些服务到.net6,所以可以使用微软提供的这个功能。

2.大量的使用到了序列化和反序列化,可以为建立一个上下文,将这这些类型通过JsonSerializable注册到上下文中,当然也可以根据领域划分多个上下文。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。