Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
bcca8458
Commit
bcca8458
authored
Oct 11, 2017
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update add jsonproperty
parent
53e83667
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
92 deletions
+102
-92
Helper.cs
src/DotNetCore.CAP/Infrastructure/Helper.cs
+102
-92
No files found.
src/DotNetCore.CAP/Infrastructure/Helper.cs
View file @
bcca8458
...
@@ -7,96 +7,106 @@ using Newtonsoft.Json.Linq;
...
@@ -7,96 +7,106 @@ using Newtonsoft.Json.Linq;
namespace
DotNetCore.CAP.Infrastructure
namespace
DotNetCore.CAP.Infrastructure
{
{
public
static
class
Helper
public
static
class
Helper
{
{
private
static
readonly
DateTime
Epoch
=
new
DateTime
(
1970
,
1
,
1
,
0
,
0
,
0
,
DateTimeKind
.
Local
);
private
static
readonly
DateTime
Epoch
=
new
DateTime
(
1970
,
1
,
1
,
0
,
0
,
0
,
DateTimeKind
.
Local
);
private
static
JsonSerializerSettings
_serializerSettings
;
private
static
JsonSerializerSettings
_serializerSettings
;
public
static
void
SetSerializerSettings
(
JsonSerializerSettings
setting
)
public
static
void
SetSerializerSettings
(
JsonSerializerSettings
setting
)
{
{
_serializerSettings
=
setting
;
_serializerSettings
=
setting
;
}
}
public
static
string
ToJson
(
object
value
)
public
static
string
ToJson
(
object
value
)
{
{
return
value
!=
null
return
value
!=
null
?
JsonConvert
.
SerializeObject
(
value
,
_serializerSettings
)
?
JsonConvert
.
SerializeObject
(
value
,
_serializerSettings
)
:
null
;
:
null
;
}
}
public
static
T
FromJson
<
T
>(
string
value
)
public
static
T
FromJson
<
T
>(
string
value
)
{
{
return
value
!=
null
return
value
!=
null
?
JsonConvert
.
DeserializeObject
<
T
>(
value
,
_serializerSettings
)
?
JsonConvert
.
DeserializeObject
<
T
>(
value
,
_serializerSettings
)
:
default
(
T
);
:
default
(
T
);
}
}
public
static
object
FromJson
(
string
value
,
Type
type
)
public
static
object
FromJson
(
string
value
,
Type
type
)
{
{
if
(
type
==
null
)
throw
new
ArgumentNullException
(
nameof
(
type
));
if
(
type
==
null
)
throw
new
ArgumentNullException
(
nameof
(
type
));
return
value
!=
null
return
value
!=
null
?
JsonConvert
.
DeserializeObject
(
value
,
type
,
_serializerSettings
)
?
JsonConvert
.
DeserializeObject
(
value
,
type
,
_serializerSettings
)
:
null
;
:
null
;
}
}
public
static
long
ToTimestamp
(
DateTime
value
)
public
static
long
ToTimestamp
(
DateTime
value
)
{
{
var
elapsedTime
=
value
-
Epoch
;
var
elapsedTime
=
value
-
Epoch
;
return
(
long
)
elapsedTime
.
TotalSeconds
;
return
(
long
)
elapsedTime
.
TotalSeconds
;
}
}
public
static
DateTime
FromTimestamp
(
long
value
)
public
static
DateTime
FromTimestamp
(
long
value
)
{
{
// ReSharper disable once ImpureMethodCallOnReadonlyValueField
// ReSharper disable once ImpureMethodCallOnReadonlyValueField
return
Epoch
.
AddSeconds
(
value
);
return
Epoch
.
AddSeconds
(
value
);
}
}
public
static
bool
IsController
(
TypeInfo
typeInfo
)
public
static
bool
IsController
(
TypeInfo
typeInfo
)
{
{
if
(!
typeInfo
.
IsClass
)
if
(!
typeInfo
.
IsClass
)
return
false
;
return
false
;
if
(
typeInfo
.
IsAbstract
)
if
(
typeInfo
.
IsAbstract
)
return
false
;
return
false
;
if
(!
typeInfo
.
IsPublic
)
if
(!
typeInfo
.
IsPublic
)
return
false
;
return
false
;
return
!
typeInfo
.
ContainsGenericParameters
return
!
typeInfo
.
ContainsGenericParameters
&&
typeInfo
.
Name
.
EndsWith
(
"Controller"
,
StringComparison
.
OrdinalIgnoreCase
);
&&
typeInfo
.
Name
.
EndsWith
(
"Controller"
,
StringComparison
.
OrdinalIgnoreCase
);
}
}
public
static
bool
IsComplexType
(
Type
type
)
public
static
bool
IsComplexType
(
Type
type
)
{
{
return
!
CanConvertFromString
(
type
);
return
!
CanConvertFromString
(
type
);
}
}
public
static
string
AddJsonProperty
(
string
json
,
IList
<
KeyValuePair
<
string
,
string
>>
properties
)
public
static
string
AddJsonProperty
(
string
json
,
IList
<
KeyValuePair
<
string
,
string
>>
properties
)
{
{
var
jObj
=
JObject
.
Parse
(
json
);
var
jObj
=
JObject
.
Parse
(
json
);
foreach
(
var
property
in
properties
)
foreach
(
var
property
in
properties
)
jObj
.
Add
(
new
JProperty
(
property
.
Key
,
property
.
Value
));
{
return
jObj
.
ToString
();
if
(
jObj
.
TryGetValue
(
property
.
Key
,
out
var
_
))
}
{
jObj
[
property
.
Key
]
=
property
.
Value
;
private
static
bool
CanConvertFromString
(
Type
destinationType
)
}
{
else
destinationType
=
Nullable
.
GetUnderlyingType
(
destinationType
)
??
destinationType
;
{
return
IsSimpleType
(
destinationType
)
||
jObj
.
Add
(
new
JProperty
(
property
.
Key
,
property
.
Value
));
TypeDescriptor
.
GetConverter
(
destinationType
).
CanConvertFrom
(
typeof
(
string
));
}
}
}
private
static
bool
IsSimpleType
(
Type
type
)
return
jObj
.
ToString
();
{
}
return
type
.
GetTypeInfo
().
IsPrimitive
||
type
==
typeof
(
decimal
)
||
private
static
bool
CanConvertFromString
(
Type
destinationType
)
type
==
typeof
(
string
)
||
{
type
==
typeof
(
DateTime
)
||
destinationType
=
Nullable
.
GetUnderlyingType
(
destinationType
)
??
destinationType
;
type
==
typeof
(
Guid
)
||
return
IsSimpleType
(
destinationType
)
||
type
==
typeof
(
DateTimeOffset
)
||
TypeDescriptor
.
GetConverter
(
destinationType
).
CanConvertFrom
(
typeof
(
string
));
type
==
typeof
(
TimeSpan
)
||
}
type
==
typeof
(
Uri
);
}
private
static
bool
IsSimpleType
(
Type
type
)
}
{
return
type
.
GetTypeInfo
().
IsPrimitive
||
type
==
typeof
(
decimal
)
||
type
==
typeof
(
string
)
||
type
==
typeof
(
DateTime
)
||
type
==
typeof
(
Guid
)
||
type
==
typeof
(
DateTimeOffset
)
||
type
==
typeof
(
TimeSpan
)
||
type
==
typeof
(
Uri
);
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment