Json反序列死循环,反射SetValue()类型Nullable<T>填坑

本文探讨了在使用C#进行JSON反序列化时遇到的死循环与内存泄露问题,通过修改原始JSON字符串格式成功解决。同时,针对反射中SetValue()方法在处理Nullable类型时的转换失败提供了实用解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

var strJson={"name":"123" };  //原始json字符

var data=JsonConvert.DeserializeObject<DataTable>(strJson); //反序列化死循环,导致内存泄露

//解决办法

//原始strJson改为  
var strJson="[{"name":"123" }]";  //重点加上[] ,即可反序列成功

//反射,SetValue()遇见int?即(Nullable<T>) 类型转换失败的解决办法

GetProperty("name").SetValue(“反射的对象”,Convert.ChangeType(“值”,(Nullable.GetUnderlyingType(propertyInfo.PropertyType))??propertyInfo.PropertyType), null)

 

<?xml version=“1.0” encoding=“UTF-8”?> <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo5</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo5</name> <description>demo5</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.6.13</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.13</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.vaadin.external.google</groupId> <artifactId>android-json</artifactId> <version>0.0.20131108.vaadin1</version> <scope>compile</scope> </dependency> <dependency> <groupId>net.minidev</groupId> <artifactId>json-smart</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.example.demo.Demo5Application</mainClass> <skip>true</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 我的依赖是这些,原有的不变给我加上fastJSON的依赖
最新发布
03-08
<think>好的,我现在需要帮用户在他们的Maven项目中添加FastJSON的依赖。首先,我得仔细看一下用户提供的pom.xml文件,了解现有的依赖情况,确保添加时不会冲突或者重复。 用户当前的依赖包括Spring Boot Starter Web、Lombok、Spring Boot Starter Test、android-jsonjson-smart。注意到json-smart没有指定版本,可能由Spring Boot的父POM管理版本。而用户现在想添加FastJSON,也就是阿里巴巴的JSON处理库,所以需要找到正确的groupId、artifactId和版本号。 接下来,我需要确定FastJSON的Maven依赖配置。通常,FastJSON的groupId是com.alibaba,artifactId是fastjson,最新版本可能需要查一下,不过用户可能希望用较新且稳定的版本,比如1.2.83或者更高,因为旧版本可能存在安全漏洞。不过用户可能没有指定版本,所以需要建议一个合适的。 然后,检查用户是否已经有其他JSON库的依赖,比如默认的Jackson。用户项目中已经引入了android-jsonjson-smart,可能用户需要切换或者兼容不同的JSON库。添加FastJSON的时候,可能需要排除Jackson,但用户没有提到这点,所以暂时先不加排除,只是单纯添加依赖。 接下来,找到正确的依赖代码段。例如: <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> 然后,需要确定在pom.xml中的哪个位置添加这个依赖。用户的依赖都放在<dependencies>标签内,所以应该放在现有的依赖项后面,比如在json-smart之后添加。 另外,还需要注意版本号是否需要通过属性管理。用户可能在<properties>里定义了版本,但当前pom中没有,所以直接写版本号即可。或者建议用户将版本定义为属性,比如<fastjson.version>1.2.83</fastjson.version>,然后在依赖中引用,这样便于统一管理。 最后,提醒用户可能需要配置Spring Boot以使用FastJSON作为默认的JSON处理器,比如通过配置类或者配置文件,替换掉默认的Jackson。不过这可能超出当前问题的范围,用户可能只需要添加依赖,所以可以简要提及如果需要使用FastJSON的特性,可能需要额外配置。 总结步骤: 1. 在<dependencies>标签内添加FastJSON的依赖项。 2. 指定正确的groupId、artifactId和版本号。 3. 确保依赖位置正确,与其他依赖并列。 4. 可选建议:使用属性管理版本号,或者提醒配置JSON处理器。 现在需要检查用户提供的pom.xml结构,确保依赖添加到正确的位置。用户当前的依赖部分如下: <dependencies> <!-- 现有的依赖项 --> </dependencies> 所以,在最后一个</dependency>标签后,添加FastJSON的依赖即可。同时,注意格式是否正确,比如缩进和标签闭合。</think>根据您提供的`pom.xml`文件,添加FastJSON依赖的步骤如下: --- ### 步骤 1:在 `<dependencies>` 标签内添加依赖 找到 `<dependencies>` 区块,在最后一个现有依赖(`json-smart`)之后插入以下代码: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> ``` --- ### 步骤 2:完整修改后的依赖部分 修改后的 `<dependencies>` 部分如下(已添加 FastJSON): ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.13</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.vaadin.external.google</groupId> <artifactId>android-json</artifactId> <version>0.0.20131108.vaadin1</version> <scope>compile</scope> </dependency> <dependency> <groupId>net.minidev</groupId> <artifactId>json-smart</artifactId> </dependency> <!-- 新增 FastJSON 依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> </dependencies> ``` --- ### 关键说明 1. **版本选择**: - 使用 `1.2.83` 是经过验证的稳定版本(注意:如果需最新版可替换为更高版本,但需检查兼容性)。 - FastJSON 的版本号建议通过 `<properties>` 统一管理(可选优化)。 2. **依赖冲突**: - 项目中已存在 `json-smart` 和 `android-json`,需确保 FastJSONJSON 解析优先级生效。若需强制使用 FastJSON,可在 Spring Boot 中配置: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new FastJsonHttpMessageConverter()); } } ``` 3. **Maven 更新**: 添加依赖后执行 `mvn clean install` 以下载 FastJSON 库。 --- ### 最终效果 - FastJSON 将提供高性能的 JSON 序列化/反序列化功能。 - 现有其他依赖(如 Spring Boot、Lombok 等)保持不变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值